2

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

2 Answers2

4

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

vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26
  • 1
    `page._client` don't work in newer versions of puppeteer. I figure out a way to change download path , find answer here https://stackoverflow.com/a/73153945/14085862 – Muhammad Uzair Jul 28 '22 at 13:49
2

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
});
  • Please can you explain this code a bit more? the questions is a "why", it needs a "because". – stramin Sep 30 '22 at 15:45
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 30 '22 at 15:46
  • @stramin This answer supports the accepted answer (the 'why' is mentioned there). I wrote this answer for newer versions of puppeteer, where `_client` is deprecated – Rajkumar Gaur Oct 06 '22 at 07:43