Skip to content

Instantly share code, notes, and snippets.

@CodeLeom
Created October 24, 2023 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeLeom/6f3055951fca57d36819feb5143187cf to your computer and use it in GitHub Desktop.
Save CodeLeom/6f3055951fca57d36819feb5143187cf to your computer and use it in GitHub Desktop.
Automating Puppeteer download functionality
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