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.

setDownloadBehavior issue

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: ^1.18.1
  • Platform / OS version: Win 10
  • Node.js version: v8.12.0

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. Run script
  2. It fails

Code:

const puppeteer = require('puppeteer');

(async () => {
   const browser = await puppeteer.launch({
      executablePath: 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
      defaultViewport: {
          width: 1920,
          height: 1080
      },
      headless: false,
      userDataDir: "./user_data"
   });

   const page = await browser.newPage();

   await page.goto(
     'https://example.com/page-with-the-file-link',
      { waitUntil: 'domcontentloaded' },
   );

   await page._client.send('Page.setDownloadBehavior', {
         behavior: 'allow',
         downloadPath: 'C:/Users/Me/Downloads/custom/folder/'
   });

   console.log('Start downloading');

   await page.click('a.download-btn');

   await page.waitFor(5000);

   console.log('Complete');
   await browser.close();
})();

What is the expected result?

Catch “download complete” event and to make it work correctly. Also I want to handle downloading status and monitor downloading proccess.

What happens instead?

Start downloading
Complete
(node:51016) UnhandledPromiseRejectionWarning: Error: WebSocket is not open: readyState 3 (CLOSED)
    at WebSocket.send (C:\Users\Me\Downloads\puppeteer\node_modules\ws\lib\websocket.js:329:19)
    at WebSocketTransport.send (C:\Users\Me\Downloads\puppeteer\node_modules\puppeteer\lib\WebSocketTransport.js:60:14)
    at Connection._rawSend (C:\Users\Me\Downloads\puppeteer\node_modules\puppeteer\lib\Connection.js:86:21)
    at Connection.send (C:\Users\Me\Downloads\puppeteer\node_modules\puppeteer\lib\Connection.js:72:21)
    at gracefullyCloseChrome (C:\Users\Me\Downloads\puppeteer\node_modules\puppeteer\lib\Launcher.js:194:20)
    at Browser.close (C:\Users\Me\Downloads\puppeteer\node_modules\puppeteer\lib\Browser.js:255:31)
    at Browser.<anonymous> (C:\Users\Me\Downloads\puppeteer\node_modules\puppeteer\lib\helper.js:112:23)
    at C:\Users\Me\Downloads\puppeteer\test-download-file.js:97:18
    at <anonymous>
(node:51016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:51016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Script does not work properly during the execution. It exits right after clicking download link on the page w/o waiting for 5 seconds and chrome puts downloaded file into default 'Downloads' folder instead of defined 'downloadPath' in the script.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
toyssamuraicommented, Jul 22, 2020

Page.setDownloadBehavior no longer works for me (07/21/2020). To work around, use createCDPSession to get a CDP Session, then do a Browser.setDownloadBehavior:

const cdpsession = await page.target().createCDPSession();
cdpsession.send ("Browser.setDownloadBehavior", {behavior:"allow", downloadPath: " ... download path ..." });

I believe that it may have to do with Page.setDownloadBehavior being deprecated.

4reactions
amunimcommented, Feb 7, 2021

I’m also getting download error, is there any way to download files by clicking?

I’ve tried both, Broswer.set... and Page.set... none of them gets the job done.

**EDIT: ** to save someone’s hour, the problem is that on windows you have to use path like this “D:\my\folder” instead of “./savepath”

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS Puppeteer setDownloadBehavior issue
I am trying to set custom path for downloading but chrome puts files into typical Downloads folder anyways. const puppeteer = require('puppeteer ...
Read more >
Page.setDownloadBehavior - Google Groups
Greetings, I'm using this call to setup the download behavior and it generally works fine. I'm downloading PDF files and they do get...
Read more >
How to download a file with Puppeteer? - ScrapingBee
However, there is one minor issue. ... setDownloadBehavior property of Puppeteer to tie up the path to Chrome browser.
Read more >
Browser.setDownloadBehavior doesn't work for WebView2
setDownloadBehavior ", { behavior: "deny" });. Is this a known issue or I am doing something wrong? WebView2 version: 0.9.579. Edge Beta version:...
Read more >
[cdp] download events do not emit on the browser domain
(follow the guides from here: https://chromedevtools.github.io/devtools-protocol/#monitor). 1. Issue the `Browser.setDownloadBehavior` command with `Main.
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