How to handle file downloads in Puppeteer?

In Puppeteer, file downloads can be handled using the page.on('response', callback) method to intercept responses and triggers events when a download is initiated. Here's a step-by-step guide on how to handle file downloads in Puppeteer:

  1. Create a Puppeteer browser instance:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Define the download path for files const downloadPath = '/path/to/download/directory'; // Intercept responses to handle file downloads page.on('response', async response => { const url = response.url(); if (url.includes('.pdf') || url.includes('.csv')) { // specify file formats to download const fileName = url.substring(url.lastIndexOf('/') + 1); const filePath = `${downloadPath}/${fileName}`; const buffer = await response.buffer(); // get response data as a buffer await fs.writeFile(filePath, buffer); // save file to the download path console.log(`File downloaded: ${filePath}`); } }); // Navigate to a page that triggers file download await page.goto('https://example.com'); // Close the browser after download is done await browser.close(); })();
  1. Replace '/path/to/download/directory' with the local directory path where you want to save the downloaded files.

  2. Modify the if condition inside the response listener to specify the file formats that you want to download. Update '.pdf' and '.csv' to match the file extensions that you are targeting.

  3. In the response listener, you can extract the file name from the download URL and save the file to the download path using fs.writeFile or any other file-saving method.

  4. Make sure to close the Puppeteer browser instance after the download is complete.

By following these steps, you can handle file downloads in Puppeteer and save the downloaded files to a specified directory.