最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer throws "UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded" so

programmeradmin3浏览0评论

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('', {waitUntil: 'networkidle2'});
    await page.screenshot({ path: 'screenshot.png' });

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

(*Snippet from Docs-Usage).

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

(node:7840) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\_test\headless\node_modules\puppeteer\lib\LifecycleWatcher.js:142:21)
    at <anonymous>
  -- ASYNC --
    at Frame.<anonymous> (C:\_test\headless\node_modules\puppeteer\lib\helper.js:111:15)
    at Page.goto (C:\_test\headless\node_modules\puppeteer\lib\Page.js:629:49)
    at Page.<anonymous> (C:\_test\headless\node_modules\puppeteer\lib\helper.js:112:23)
    at C:\_test\headless\index.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 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

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.', {waitUntil: 'networkidle2'});
    await page.screenshot({ path: 'screenshot.png' });

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

(*Snippet from Docs-Usage).

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

(node:7840) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\_test\headless\node_modules\puppeteer\lib\LifecycleWatcher.js:142:21)
    at <anonymous>
  -- ASYNC --
    at Frame.<anonymous> (C:\_test\headless\node_modules\puppeteer\lib\helper.js:111:15)
    at Page.goto (C:\_test\headless\node_modules\puppeteer\lib\Page.js:629:49)
    at Page.<anonymous> (C:\_test\headless\node_modules\puppeteer\lib\helper.js:112:23)
    at C:\_test\headless\index.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. 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
Share Improve this question asked Jun 25, 2019 at 15:50 francofranco 1122 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I encountered the same issue but solved it by replacing

{waitUntil: 'networkidle2'}

with:

{waitUntil: 'domcontentloaded'}

More information here:

[https://github./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./', {
waitUntil: 'networkidle2'
});

Sometimes servers do not respond to headless requests (puppeteer #2963).
To verify this, I'd try run the code in debug mode:

DEBUG=*session node your-test-file.js

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论