最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why Puppeteer is not downloading into folder specified with "Page.setDownloadBehavior" in Windows

programmeradmin2浏览0评论

I use the following piece of code to achieve the download of a file by making Puppeteer click the button that emits the event

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

It works pretty fine in a Mac OS machine, but it does nothing in a Windows one

The try/catch that wraps this block it doesn't catch anything, and when I use the headless: false setting, it's proven to "download the file", but there is no file in all the hard drive

I use the following piece of code to achieve the download of a file by making Puppeteer click the button that emits the event

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

It works pretty fine in a Mac OS machine, but it does nothing in a Windows one

The try/catch that wraps this block it doesn't catch anything, and when I use the headless: false setting, it's proven to "download the file", but there is no file in all the hard drive

Share asked Feb 20, 2019 at 20:07 Benjamín VázquezBenjamín Vázquez 3504 silver badges12 bronze badges 5
  • did you try '.\\temporal\\files\\' ? – hardkoded Commented Feb 20, 2019 at 20:22
  • Actually I did, it doesn't changed anything – Benjamín Vázquez Commented Feb 20, 2019 at 20:23
  • Maybe Windows API needs an absolute path? – vsemozhebuty Commented Feb 20, 2019 at 20:25
  • 1 @vsemozhetbyt You are right!, please post the answer so I can voted!!! – Benjamín Vázquez Commented Feb 20, 2019 at 20:30
  • I've posted it :) – vsemozhebuty Commented Feb 20, 2019 at 20:35
Add a ment  | 

2 Answers 2

Reset to default 4

In case Windows API needs an absolute path, you can pose it with __dirname and relative path.

For newer versions (tested in version 12.0.1 on Windows)

page._client is deprecated, instead, use page.client()

downloadPath needs to be absolute for Windows (see the accepted answer)

await page.client().send("Page.setDownloadBehavior", {
  behavior: "allow",
  downloadPath: __dirname + "./", // referenced from the accepted answer
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论