Hossain Ahamed
Hossain Ahamed

Reputation: 119

Puppeteer chrome allow multiple file download

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);
}

Upvotes: 6

Views: 2661

Answers (3)

viv richards
viv richards

Reputation: 11

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.

Upvotes: 0

Muhammad Uzair
Muhammad Uzair

Reputation: 484

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,
        },
      },
    },
  })
);

Upvotes: 1

Ankush Sharma
Ankush Sharma

Reputation: 41

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.

Upvotes: 4

Related Questions