I’m working on a project with Puppeteer and I’ve managed to download files successfully. But there’s a problem. The files always end up in my default Downloads folder. I’ve been searching everywhere for a way to change the download location in Puppeteer. The API docs and forums haven’t been much help.
Right now, my download code is super simple:
await page.navigate(fileUrl);
Does anyone know how to tell Puppeteer where to save downloaded files? I’d love to set a custom folder for my project’s downloads. Any tips or code examples would be awesome. Thanks!
hey flyingleaf, i’ve been there too! heres a quick trick that worked for me:
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: '/your/custom/path'
});
just swap out the path with where u want ur files. hope this helps ya out!
I’ve tackled this issue before in one of my projects. The key is to set the custom downloads path at launch, though note that Puppeteer doesn’t officially support a ‘downloadsPath’ option directly, and you might need a workaround depending on your Puppeteer version. In my experience, configuring the download directory involves either launching the browser with specific flags or intercepting downloads via the CDP (Chrome DevTools Protocol). Please ensure your specified folder exists and is accessible. Adjust the configuration based on your environment; sometimes creating the folder programmatically before the download process is the best approach.
I’ve dealt with this exact problem in a recent project. What worked for me was using the ‘downloadPath’ option when launching the browser. Here’s a snippet that might help:
const browser = await puppeteer.launch({
defaultViewport: null,
args: ['--no-sandbox'],
downloadsPath: '/path/to/your/custom/folder'
});
This sets a custom download directory for all downloads initiated by Puppeteer. Just replace ‘/path/to/your/custom/folder’ with your desired path. Remember to use absolute paths to avoid any issues.
If that doesn’t work, you might need to use the Page.setDownloadBehavior method:
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: '/path/to/your/custom/folder'
});
Hope this helps you out. Let me know if you need any clarification!