How to Download a File With Puppeteer?

Downloading files with Puppeteer is straightforward. Below is an example of how to do it efficiently without unnecessary complexity.

Here, we will automate the download of a file by setting the download behavior and specifying the download path.

      const puppeteer = require('puppeteer');
const path = require('path');

(async () => {
  const downloadPath = path.resolve('./download'); // Define download path
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();

  // Set download behavior and path
  await page._client.send('Page.setDownloadBehavior', {
    behavior: 'allow',
    downloadPath: downloadPath 
  });

  await page.goto('https://example.com/download', { waitUntil: 'networkidle2' });

  // Click on the download button
  await page.click('#downloadButton'); // Adjust the selector as needed

  console.log('File download initiated.');
  await browser.close();
})();

    

Explanation:

  1. Download Path: Set the path where the file will be downloaded using path.resolve.
  2. Browser Launch: Initiate a headless browser instance.
  3. Set Download Behavior: Configure Puppeteer to allow downloads and specify the download directory.
  4. Navigate to URL: Go to the webpage containing the download link.
  5. Simulate Click: Click the download button to initiate the file download.

For more advanced file handling, you can refer to Bright Data’s Scraping Browser, which offers enhanced capabilities like automatic CAPTCHA solving and handling complex website interactions, making it ideal for extensive web scraping projects.

Ready to get started?