MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / downloadFile

Method downloadFile

javascript/selenium-webdriver/lib/webdriver.js:1751–1782  ·  view source on GitHub ↗
(fileName, targetDirectory)

Source from the content-addressed store, hash-verified

1749 }
1750
1751 async downloadFile(fileName, targetDirectory) {
1752 const caps = await this.getCapabilities()
1753 if (!caps['map_'].get('se:downloadsEnabled')) {
1754 throw new Error('Downloads must be enabled in options')
1755 }
1756
1757 const response = await this.execute(new command.Command(command.Name.DOWNLOAD_FILE).setParameter('name', fileName))
1758
1759 const base64Content = response.contents
1760
1761 if (!targetDirectory.endsWith('/')) {
1762 targetDirectory += '/'
1763 }
1764
1765 fs.mkdirSync(targetDirectory, { recursive: true })
1766 const zipFilePath = path.join(targetDirectory, `${fileName}.zip`)
1767 fs.writeFileSync(zipFilePath, Buffer.from(base64Content, 'base64'))
1768
1769 const zipData = fs.readFileSync(zipFilePath)
1770 await JSZip.loadAsync(zipData)
1771 .then((zip) => {
1772 // Iterate through each file in the zip archive
1773 Object.keys(zip.files).forEach(async (fileName) => {
1774 const fileData = await zip.files[fileName].async('nodebuffer')
1775 fs.writeFileSync(`${targetDirectory}/${fileName}`, fileData)
1776 console.log(`File extracted: ${fileName}`)
1777 })
1778 })
1779 .catch((error) => {
1780 console.error('Error unzipping file:', error)
1781 })
1782 }
1783
1784 async deleteDownloadableFiles() {
1785 const caps = await this.getCapabilities()

Callers

nothing calls this directly

Calls 9

getCapabilitiesMethod · 0.95
executeMethod · 0.95
setParameterMethod · 0.80
getMethod · 0.65
logMethod · 0.65
fromMethod · 0.45
forEachMethod · 0.45
keysMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected