Created
October 24, 2023 02:48
-
-
Save CodeLeom/c5ce35fec15359786045208ddc24a4e0 to your computer and use it in GitHub Desktop.
Code Sample to handle Upload and Download action with Puppeteer
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' | |
//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