MCPcopy Index your code
hub / github.com/SeleniumHQ/selenium / downloadFile

Method downloadFile

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

Source from the content-addressed store, hash-verified

1704 }
1705
1706 async downloadFile(fileName, targetDirectory) {
1707 const caps = await this.getCapabilities()
1708 if (!caps['map_'].get('se:downloadsEnabled')) {
1709 throw new Error('Downloads must be enabled in options')
1710 }
1711
1712 const response = await this.execute(new command.Command(command.Name.DOWNLOAD_FILE).setParameter('name', fileName))
1713
1714 const base64Content = response.contents
1715
1716 if (!targetDirectory.endsWith('/')) {
1717 targetDirectory += '/'
1718 }
1719
1720 fs.mkdirSync(targetDirectory, { recursive: true })
1721 const zipFilePath = path.join(targetDirectory, `${fileName}.zip`)
1722 fs.writeFileSync(zipFilePath, Buffer.from(base64Content, 'base64'))
1723
1724 const zipData = fs.readFileSync(zipFilePath)
1725 await JSZip.loadAsync(zipData)
1726 .then((zip) => {
1727 // Iterate through each file in the zip archive
1728 Object.keys(zip.files).forEach(async (fileName) => {
1729 const fileData = await zip.files[fileName].async('nodebuffer')
1730 fs.writeFileSync(`${targetDirectory}/${fileName}`, fileData)
1731 console.log(`File extracted: ${fileName}`)
1732 })
1733 })
1734 .catch((error) => {
1735 console.error('Error unzipping file:', error)
1736 })
1737 }
1738
1739 async deleteDownloadableFiles() {
1740 const caps = await this.getCapabilities()

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected