Puppeteer chrome allow multiple file download
Asked Answered
S

3

6

I am trying scrape one site will multiple pdf files. But after downloading the first one, chrome asks me to allow the site to download multiple files. Download multiple files

Can we allow this while launching the browser? This is the code which works once I allow manually and downloads all the files.

for(selector of selectors){
   await this.currentPage._client.send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: downloadFilePath });
   await this.currentPage.waitFor(1000);
   await this.currentPage.evaluate(el => el.click(), selector);
   await this.currentPage.waitFor(1000 * 20);
}
Sneakers answered 28/2, 2019 at 6:16 Comment(0)
B
4

to avoid the allow multiple download popup use following code :

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

The downloadPath is the path where you want to download the files. You need to do it for each page you open.

Burgomaster answered 1/9, 2019 at 11:32 Comment(1)
this doesn't work in the latest version of puppeteer. page._client is not function.Meredithmeredithe
M
1

2022 Update: page._client method has been removed in the latest version of the puppeteer. (I am using 15.x

I manage to set custom download path & override allow-multiple-download popup using puppeteer-extra with puppeteer-extra-plugin-user-preferences plugin.

const puppeteer = require("puppeteer-extra")
const UserPreferencesPlugin = require("puppeteer-extra-plugin-user-preferences");

const downloadImageDirectoryPath = process.cwd();

puppeteer.use(
  UserPreferencesPlugin({
    userPrefs: {
      download: {
       // this one handle first time downlaod popup
        prompt_for_download: false,
        open_pdf_in_system_reader: true,
        default_directory: downloadImageDirectoryPath,
        // this arg handle multiple download popup
        automatic_downloads: 1,
      },
      plugins: {
        always_open_pdf_externally: true,
      },
    },
  })
);

Update a week later above snippet was working but unreliable so I updated my code to below snippet, no issues so far.

puppeteer.use(
  UserPreferencesPlugin({
    userPrefs: {
      download: {
        prompt_for_download: false,
        open_pdf_in_system_reader: true,
        default_directory: downloadImageDirectoryPath,
        // automatic_downloads: 1,
      },
      plugins: {
        always_open_pdf_externally: true,
      },
      // disable allow-multiple-downloads popup
      profile: {
        default_content_setting_values: {
          automatic_downloads: 1,
        },
      },
    },
  })
);
Meredithmeredithe answered 29/7, 2022 at 9:12 Comment(0)
H
0

Your original code needs a slight tweak and it should work:

    await this.currentPage._client.send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: downloadFilePath });

Try changing this to

    await this.currentPage._client().send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: downloadFilePath });

This should hopefully resolve the issue for you.

Hangout answered 3/11, 2022 at 16:26 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Sundog

© 2022 - 2024 — McMap. All rights reserved.