Setting custom download directory in Puppeteer

Hey everyone, I’m working on a project using Puppeteer and I’ve run into a bit of a snag. I’ve managed to download files successfully, but they’re all ending up in my default Downloads folder. I’ve been searching high and low for a way to change the download location, but I can’t seem to find anything in the Puppeteer docs or on any forums.

Right now, my download code is super simple:

await page.goto(downloadUrl);

This works fine, but I really need to save these files somewhere specific. Has anyone figured out how to set a custom download directory in Puppeteer? I’d really appreciate any tips or code examples you could share. Thanks in advance for your help!

hey mate, i had the same problem. try using the client.send method like this:

await page._client.send(‘Page.setDownloadBehavior’, {
behavior: ‘allow’,
downloadPath: ‘C:/your/custom/path’
});

worked for me. just remember to use forward slashes in the path. good luck!

I’ve faced this exact issue before, and I can tell you it’s not as straightforward as you’d hope. The trick is to use the downloadPath option when launching the browser. Here’s what worked for me:

const browser = await puppeteer.launch({
  headless: true,
  defaultViewport: null,
  args: ['--no-sandbox', '--disable-setuid-sandbox'],
  downloadsPath: '/path/to/your/custom/directory'
});

Make sure to replace ‘/path/to/your/custom/directory’ with the actual path where you want the files to be saved. This method has been reliable for me across different projects. One caveat: if you’re working with multiple pages or contexts, you might need to set this for each one. It can be a bit finicky, but once you get it set up, it works like a charm. Hope this helps!

I’ve encountered this issue as well, and there’s actually a more robust solution using the Page.setDownloadBehavior method. Here’s how you can implement it:

await page._client.send('Page.setDownloadBehavior', {
  behavior: 'allow',
  downloadPath: '/path/to/your/custom/directory'
});

This approach gives you more control and works consistently across different scenarios. It’s especially useful if you need to change the download directory dynamically during runtime.

Remember to replace the path with your desired download location. Also, ensure that the directory exists before running your script, as Puppeteer won’t create it automatically.

This method has proven reliable in my projects, even when dealing with multiple pages or complex download scenarios. Give it a try and see if it resolves your issue.