首页 > 解决方案 > Puppeteer chrome 允许多个文件下载

问题描述

我正在尝试抓取一个站点将多个 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);
}

标签: node.jsweb-scrapingdownloadchromiumpuppeteer

解决方案


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

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

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


推荐阅读