Skip to content

Instantly share code, notes, and snippets.

@CodeLeom
Created October 24, 2023 02:48
Show Gist options
  • Save CodeLeom/c5ce35fec15359786045208ddc24a4e0 to your computer and use it in GitHub Desktop.
Save CodeLeom/c5ce35fec15359786045208ddc24a4e0 to your computer and use it in GitHub Desktop.
Code Sample to handle Upload and Download action with Puppeteer
import puppeteer from 'puppeteer'
//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();
const client = await page.target().createCDPSession();
await client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: './downloads'
});
await page.goto('https://imgur.com/upload');
const uploadSelector = '#file-input';
await page.waitForSelector(uploadSelector);
const inputUploadHandle = await page.$(uploadSelector);
await inputUploadHandle.uploadFile('./cap.jpeg');
//wait for the upload to be completed
await delay(10000);
// initiate the download process and click the download button
const downloadLinkSelector = '.upload-download';
await page.waitForSelector(downloadLinkSelector);
await page.click(downloadLinkSelector);
//wait for the file download to
await delay(10000);
await browser.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment