Skip to content
Advertisement

Puppeteer throws “UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded” sometimes

I’m testing Headless Chrome with Puppeteer, so I’ve reading docs and running this code*:

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.github.com', {waitUntil: 'networkidle2'});
    await page.screenshot({ path: 'screenshot.png' });

    await browser.close();
})();

(*Snippet from Docs-Usage).

I changed “example.com” because works fine and trying with other sites, but with “github.com” script returns an timeout exception in the await page.goto() line:

(node:7840) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:_testheadlessnode_modulespuppeteerlibLifecycleWatcher.js:142:21)
    at <anonymous>
  -- ASYNC --
    at Frame.<anonymous> (C:_testheadlessnode_modulespuppeteerlibhelper.js:111:15)
    at Page.goto (C:_testheadlessnode_modulespuppeteerlibPage.js:629:49)
    at Page.<anonymous> (C:_testheadlessnode_modulespuppeteerlibhelper.js:112:23)
    at C:_testheadlessindex.js:7:16
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:7840) 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:7840) [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.

If I go to github.com with my regular browser, connects in normal timing, so my internet connection is not an issue.

I’ve added the next line and code runs fine after two minutes:

page.setDefaultNavigationTimeout(0);

But if I set puppeteer.launch({headless:false}) the code runs perfect in just a few seconds

I’m runing my test under:

  • Windows 7 Professional SP1
  • Node 8.11.1
  • Puppeteer 1.18.0
  • Puppeteer Core 1.18.0

Advertisement

Answer

I encountered the same issue but solved it by replacing

{waitUntil: 'networkidle2'}

with:

{waitUntil: 'domcontentloaded'}

More information here:

[https://github.com/puppeteer/puppeteer/issues/2482][1]

Just to add more information, some sites have a lot of content and so the page might not have loaded within the default timeout of 30 seconds. So do away with this, you can totally remove timeout by doing so:

await page.setDefaultNavigationTimeout(0)

So this can look like this:

const puppeteer = require('puppeteer');

(async () => {

const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(0);
await page.goto('https://www.example.com/', {
waitUntil: 'networkidle2'
});
Advertisement