puppeteer - how to set download location

14,928

Solution 1

This is how you can set the download path in latest puppeteer v0.13.

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

The behaviour is experimental, it might be removed, modified, or changed later.

Pst, you can try more tricks listed here, on your own risk :).

Solution 2

I realize this is an old thread, but this thread popped up first for me when looking for how to set Puppeteer default download location. I was able to set the download location using the following code,

let customChrome = path.resolve(__dirname, './customChrome')
        let prefs = fs.readFileSync(customChrome+'/Default/Preferences');
        let obj = JSON.parse(prefs);
        obj.savefile.default_directory = path.resolve(__dirname, './downloads');
        obj.download.default_directory = path.resolve(__dirname, './downloads');
        fs.writeFileSync(customChrome+'/Default/Preferences', JSON.stringify(obj));
        const browser = await puppeteer.launch({
            userDataDir:customChrome,
            headless: false,                                                                                                                                                                                                                                                 
            args:['--disable-features=site-per-process','--no-sandbox']
        });

This will set the default download directory for files before the process starts. Essentially, Puppeteer creates a custom profile each time it runs, we can override that profile and define the download directory.

The first time you run the above code, you will have to comment out the fs.readFile to fs.writeFile as the UserDirDirectory is created if it does not exist the first time that Chrome is started.

All profile related data is then stored in the customChrome/Default folder. How to pass userDataDir profile folder to Puppeteer

Share:
14,928

Related videos on Youtube

A. L
Author by

A. L

Updated on June 05, 2022

Comments

  • A. L
    A. L about 2 years

    I was able to successfully download a file with puppeteer, but it was just saving it to my /Downloads folder. I've been looking around and can't find anything in the api or forums to set this location.

    My downloads are basically just go going to the link:

    await page.goto(url);
    
  • Janusz01
    Janusz01 over 4 years
    Not able to change the download folder. I am placing this line before the download action and providing absolute download path. What am I doing wrong?
  • Md. Abu Taher
    Md. Abu Taher over 4 years
    Share your code somewhere, probably in a github gist.