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.

Page.goto() always causes a Navigation Timeout - but i can get screenshot somehow

See original GitHub issue

Steps to reproduce

  • Puppeteer version: 1.2.0
  • Platform / OS version: Windows 7 x64
  • Node.js version: 8.11.1

What steps will reproduce the problem? I am trying this code:

// <-- add event on top of file
process.on("unhandledRejection", (reason, p) => {
        console.error("Unhandled Rejection at: Promise", p, "reason:", reason);
        // browser.close(); // <-- no need to close the browser here
});

const puppeteer = require('puppeteer');
async function getPic() {
    try{ // <-- wrap the whole block in try catch
      const browser = await puppeteer.launch(/*{headless: false}*/);
      const page = await browser.newPage();
      await page.setViewport({width: 1000, height: 500}); // <-- add await here so it sets viewport after it creates the page


      //await page.goto('https://www.google.com');  //Old way of doing. It doesn't work for some reason...

      page.goto('https://www.google.com/'); // async


      // wait for either of events to trigger
      await Promise.race([
        page.waitForNavigation({waitUntil: 'domcontentloaded'}),
        page.waitForNavigation({waitUntil: 'load'})
      ]);


      await page.screenshot({path: 'pic.png'});
      await browser.close(); // <-- close browser after everything is done
    } catch (error) {
      console.log(error);
    }
}

getPic();

I request a pic from the page loaded for testing purposes.

With code as it is, i get pic and this error:

Unhandled Rejection at: Promise Promise {
  <rejected> Error: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous> } reason: Error: Navigation Timeout Exceeded: 30000ms exceede
d
    at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous>

When i remove the process.on() code in the beginning, i get no pic and this error message:

    at Promise.then (C:\...\pupet test\node_modules\puppeteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous>
(node:5548) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded
: 30000ms exceeded
    at Promise.then (C:\...\pupet test\node_modules\puppeteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous>
(node:5548) 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: 1)
(node:5548) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
 Node.js process with a non-zero exit code.

When process.on() IS there but instead i use await

page.goto('https://www.google.com');

INSTEAD OF

      page.goto('https://www.google.com/'); // async


      // wait for either of events to trigger
      await Promise.race([
        page.waitForNavigation({waitUntil: 'domcontentloaded'}),
        page.waitForNavigation({waitUntil: 'load'})
      ]);

i don’t get the picture and i get this:

Error: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous>

Finally, when i remove process.on() and i use await

page.goto('https://www.google.com');

INSTEAD OF

page.goto('https://www.google.com/'); // async


      // wait for either of events to trigger
      await Promise.race([
        page.waitForNavigation({waitUntil: 'domcontentloaded'}),
        page.waitForNavigation({waitUntil: 'load'})
      ]);

i don’t get the picture and i get this:

Error: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous>

In conclusion, whenever i don’t use process.on() i don’t have my code executed.

But when i do use process.on() i get the screenshot but i still get the error. Now i know that the ‘unhandledRejection’ event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop.

Now, when i use :

page.goto('https://www.google.com/').catch(error => console.log("this isn't handled by the try catch since you don't await it", error));

for troubleshooting purposes, i get:

this isn't handled by the try catch since you don't await it Error: Navigation T imeout Exceeded: 30000ms exceeded at Promise.then (C:\Users\User1\Desktop\fb proj\pupet test\node_modules\pupp eteer\lib\NavigatorWatcher.js:71:21) at <anonymous>

Which means page.goto() is the culprit. No matter what i try i cannot seem to make it work. It appears to be completely illogical, how come it throws an error but still manages to get the pic?

If i try stuff like:

const puppeteer = require('puppeteer');

async function getPic() {
	  try { // <-- wrap the whole block in try catch
	      const browser = await puppeteer.launch(/*{headless: false}*/);
	      const page = await browser.newPage();
	      await page.setViewport({width: 1000, height: 500}); // <-- add await here so it sets viewport after it creates the page
	      await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
      	  await page.setExtraHTTPHeaders({
        		'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8'
      	  });
	      await page.goto('https://www.google.com/', {waitUntil: 'load'})
	      await page.screenshot({path: 'pic.png'});
	      await browser.close(); // <-- close browser after everything is done
   		} catch (error) {
      		console.log(error);
    	}
}

getPic();

Then i just get the error without any picture.

I should also add, that i was contacted by the guy who wrote the unhandledRejection for node and told me that i shouldn’t put it in my code. But wihtout it… it doesn’t work at all!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
knownasilyacommented, Aug 8, 2018

For anyone else having a similar issue, I solved it by adding a protocol (http://) to the url, otherwise it timesout when creating a pdf (but works with headless: false).

1reaction
flycatrixcommented, Dec 5, 2019

This #2504 (comment) does not work in my case. pptr 1.8.0 node 10.11.0 x64 Win7 x64

However if headless mode is disabled the code works fine.

I have the same problem, have you resolve it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer always times out while trying to take a screenshot ...
I'm running node version 8.16.0. I have no idea why I always get this timeout. Any help is appreciated. EDIT: It does seem...
Read more >
How to solve Puppeteer TimeoutError: Navigation timeout of ...
The setDefaultNavigationTimeout method available on a created page of Puppeteer allows you to define the timeout of the tab and expects as first...
Read more >
SeleniumLibrary - Robot Framework
Captures a screenshot from the element identified by locator and embeds it into log file. See Capture Page Screenshot for details about filename ......
Read more >
navigation failed because page was closed! - You.com
If I change the uri to example.com, this error does not occur. I need to be able to navigate to the settings URI...
Read more >
Is Google Tag Manager Preview Mode Not Working? 22 Ways ...
It must be added to the site exactly as you have copied it from the GTM interface. #3. You Have Enabled GTM Preview...
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