代码之家  ›  专栏  ›  技术社区  ›  Philipp M

node.js puppeteer-如何设置导航超时?

  •  4
  • Philipp M  · 技术社区  · 7 年前

    我正在使用node.js和puppeteer获取一些数据。我打开的一些文件相当大…然后我得到一个错误:

    错误:

    our error { TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
        at Promise.then (/project/node_modules/puppeteer/lib/NavigatorWatcher.js:74:21)
        at <anonymous> name: 'TimeoutError' }
    

    如何忽略它或设置更高的超时?

    这是我的剧本:

    await page.goto('url'+tableCell04Val, {waitUntil: 'load'});
    
    const records = await page.evaluate( () =>
    {
      const page = document.createElement( 'html' );
      const page_content = document.body.textContent;
    
      page.innerHTML = page_content;
    
        return {
            'valueA' : Array.from( page.getElementsByTagName( 'valueA' ), e => e.textContent ),
            'valueB' : Array.from( page.getElementsByTagName( 'valueB' ), e => e.textContent ),
            'valueC' : Array.from( page.getElementsByTagName( 'valueC' ), e => e.textContent ),
            'valueD' : Array.from( page.getElementsByTagName( 'valueD' ), e => e.textContent )
        };
    });
    
    2 回复  |  直到 6 年前
        1
  •  10
  •   James Gould    7 年前

    你可以用 timeout: 0 如果要加载一个大页面,则禁用超时错误。

    在你的 page.goto 像:

    await page.goto('url'+tableCell04Val, {waitUntil: 'load', timeout: 0});
    

    You can see the PR made to Pupeteer here which added the change, along with documentation and the unit tests that implement it.

        2
  •  2
  •   Henry    6 年前

    您可以这样设置超时

    await page.goto('url'+tableCell04Val, {waitUntil: 'load', timeout: 10000}).then(() => {
        console.log('success')
    }).catch((res) => {
        console.log('fails', res)
    })