Puppeteer chrome 允许多个文件下载

Hos*_*med 6 download chromium node.js web-scraping puppeteer

我正在尝试抓取一个网站上的多个 pdf 文件。但下载第一个后,chrome 要求我允许该网站下载多个文件。 下载多个文件

我们可以在启动浏览器时允许这样做吗?一旦我手动允许并下载所有文件,这个代码就可以工作。

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);
}
Run Code Online (Sandbox Code Playgroud)

小智 4

为避免允许多次下载弹出窗口,请使用以下代码:

await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './'});
Run Code Online (Sandbox Code Playgroud)

downloadPath 是您要下载文件的路径。您需要为打开的每个页面执行此操作。