Puppeteer 有时会抛出“UnhandledPromiseRejectionWarning:TimeoutError:导航超时超出”

fra*_*nco 2 javascript node.js puppeteer

我正在使用 Puppeteer 测试 Headless Chrome,因此我阅读了文档并运行了以下代码*:

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();
})();
Run Code Online (Sandbox Code Playgroud)

(*来自Docs-Usage的片段)。

我更改了“example.com”,因为工作正常并尝试与其他站点一起使用,但使用“github.com”脚本会在行中返回超时异常await page.goto()

(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.
Run Code Online (Sandbox Code Playgroud)

如果我使用常规浏览器访问 github.com,会按正常时间连接,所以我的互联网连接不是问题。

我添加了下一行,代码在两分钟后运行良好:

(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.
Run Code Online (Sandbox Code Playgroud)

但如果我设置puppeteer.launch({headless:false})代码在短短几秒钟内运行完美

我正在以下下运行测试:

  • Windows 7 专业版 SP1
  • 节点8.11.1
  • 傀儡师1.18.0
  • 傀儡师核心 1.18.0

Sim*_*tia 6

我遇到了同样的问题,但通过替换解决了

{waitUntil: 'networkidle2'}
Run Code Online (Sandbox Code Playgroud)

和:

{waitUntil: 'domcontentloaded'}
Run Code Online (Sandbox Code Playgroud)

更多信息请点击这里:

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

只是为了添加更多信息,某些网站有大量内容,因此页面可能无法在 30 秒的默认超时内加载。因此,消除这个问题,您可以通过这样做完全消除超时:

await page.setDefaultNavigationTimeout(0)
Run Code Online (Sandbox Code Playgroud)

所以这可以看起来像这样:

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'
});
Run Code Online (Sandbox Code Playgroud)