question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Puppeteer how to save/set download path when download starts from different tab not the current one

See original GitHub issue

Hello In my code i am trying to download file. When puppeteer clicks on a tag it triggers download but also opens new tab for 2-3sec where this download is held (i think). SO i thinkl i need to set download behaviour to that tab but problme is it flashes and browers.pages() also did not catch it. SO how can i set different folder for this download? Tell us about your environment:

  • Puppeteer version: 1,17.0
  • Platform / OS version: Windows 10
  • URLs (if applicable): no
  • Node.js version: Fresh instalation

What steps will reproduce the problem?

const client = await page.target().createCDPSession(); await client.send("Page.setDownloadBehavior", { behavior: "allow", downloadPath: path.resolve(__dirname, "reports/asd") });

Please include code that reproduces the issue.

async function main() {
      const browser = await puppeteer.launch({ headless: false });
      const page = await browser.newPage();
      await page.setViewport({ width: 1366, height: 800 });
      await page.goto(endpoint, { waitUntil: "networkidle2", timeout: 0 });
      await page.waitFor(3000);

      await page.type("#username", login);
      await page.type("#password", pass);
      // click and wait for navigation

      await page.click("#login"),
        await page.waitForNavigation({ waitUntil: "networkidle2" });
      await page.waitFor(2000);

       const client = await page.target().createCDPSession();
  await client.send("Page.setDownloadBehavior", {
    behavior: "allow",
    downloadPath: path.resolve(__dirname, "reports/asd")
  });

      await page.waitForXPath('//*[@id="ctl00_ctl00_masterMain_cphMain_repViewable_ctl01_thisItemLink"]');

      const [setting] = await page.$x('//*[@id="ctl00_ctl00_masterMain_cphMain_repViewable_ctl01_thisItemLink"]');
      if (setting) setting.click();

      await page.waitFor(3000);

      let pages = await browser.pages();

      await page.waitFor(2000);

      await pages[2].waitForXPath('/html/body/table/tbody/tr[17]/td[6]/a');
      const [newVID] = await pages[2].$x('/html/body/table/tbody/tr[17]/td[6]/a');
      if (newVID) newVID.click();

     await pages[2].waitFor(3000)

     await pages[2].close()

     await pages[2].waitFor(5000)

     await pages[2].waitForXPath('//*[@id="freeze"]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/a');
     const [neww] = await pages[2].$x('//*[@id="freeze"]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/a');
     if (neww) neww.click();

    await pages[2].waitFor(4000)

    await pages[2].waitFor(4000)

    await pages[2].waitForXPath('/html/body/div/table/tbody/tr[3]/td/a');
    const [fin] = await pages[2].$x('/html/body/div/table/tbody/tr[3]/td/a');
    if (fin) fin.click();

    }

    main() 

What is the expected result?

to download file to folder set in code not to default downloads

What happens instead?

file is save to downloads

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

13reactions
derekylecommented, Sep 26, 2019

I wasn’t able to figure out a way to set a default downloads folder (why this isn’t a setting on the browser object is beyond me). So what I did was use page.exposeFunction() to override the window.open function on the page that will try to open the new tab. Then in the callback, I used page.goto() to go to the download URL within the same tab instead of opening a new one. It had to be enclosed in a try/catch because it throws ERR_ABORTED when the file being browsed to is a download.

await page.exposeFunction('open', async (open) => {
            console.log(open);
            try {
                await page.goto('https://www.myURL.com' + open);
            } catch (err) {
                console.log(err);
            } finally {
                console.log('done')
            }
        });
7reactions
Fritzsierracommented, Sep 6, 2020

Using Puppeteer Extra you can configure the folder where you want to download:

`const puppeteer = require(“puppeteer-extra”);

var dir = process.cwd () puppeteer.use(require(‘puppeteer-extra-plugin-user-preferences’) ( {userPrefs: { download: { prompt_for_download: false, open_pdf_in_system_reader: true, default_directory:dir, }, plugins: { always_open_pdf_externally: true }, }}));`

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - puppeteer - how to set download location
This is how you can set the download path in latest puppeteer v0.13. await page._client.send('Page.setDownloadBehavior', {behavior: 'allow' ...
Read more >
How to download a file with Puppeteer? - ScrapingBee
In this article, we will discuss how to efficiently download files with Puppeteer. Automating file downloads can sometimes be complicated.
Read more >
Dealing with file downloads in puppeteer - browserless docs
1. Mount the WORKSPACE_DIR someplace. · 2. Tell puppeteer where to download files. · 3. Download the file in the page · 4....
Read more >
How to Configure Custom Download Folder in Puppeteer
The below code will set the download path to the download folder under the root project directory. // Launch a new browser. const...
Read more >
Release 22.11.0 Carsten Rose, Benjamin Baer
download to debug. Note: Converting HTML to PDF gives no error message but RC=-1? Check carefully all includes of CSS, JS, images and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found