Created
October 24, 2023 02:53
-
-
Save CodeLeom/6f3055951fca57d36819feb5143187cf to your computer and use it in GitHub Desktop.
Automating Puppeteer download functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import puppeteer from 'puppeteer' | |
import * as fs from 'fs'; | |
//function to handle timeout | |
function delay(time) { | |
return new Promise(resolve => setTimeout(resolve, time)); | |
} | |
const browser = await puppeteer.launch({ | |
headless: false | |
}); | |
const page = await browser.newPage(); | |
// Set download behavior | |
const client = await page.target().createCDPSession() | |
await client.send('Page.setDownloadBehavior', { | |
behavior: 'allow', | |
// the download path can be set to a folder in your project root | |
downloadPath: './downloads' | |
}); | |
// Navigate to the download page. | |
await page.goto('https://easyupload.io/x2na1r'); | |
// Download the file. | |
await page.click('#hd1'); | |
// Wait for the download to complete. Adjust this based on your network speed. | |
await delay(10000); | |
//You will be checking the bank-statement folder to know if the downloaded file exists there | |
if (fs.existsSync('./downloads/testdoc.pdf')) { | |
console.log('file downloaded successfully!'); | |
} else { | |
console.log('Download failed.'); | |
} | |
await browser.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment