简体   繁体   English

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

[英]Node.js puppeteer - How to set navigation timeout?

I'm using node.js and puppeteer to get some data.我正在使用 node.js 和 puppeteer 来获取一些数据。 Some of the files I'm opening are quite large ... and then I get an error:我打开的一些文件非常大......然后我收到一个错误:

Error:错误:

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

How can I ignore it or set a higher timeout?如何忽略它或设置更高的超时时间?

That's my script:这是我的脚本:

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

You can use timeout: 0 to disable timeout errors if you're loading a heavy page.如果您正在加载大量页面,您可以使用timeout: 0来禁用超时错误。

Use it in your page.goto like:在您的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. 您可以在此处查看对 Pupeteer 所做的 PR,其中添加了更改,以及实现它的文档和单元测试。

UPDATE 2019 2019 年更新

You can also change the page behaviour since V1.0.0:您还可以更改自 V1.0.0 以来的页面行为:

await page.setDefaultNavigationTimeout(0); 

The param is the timeout in milliseconds.参数是以毫秒为单位的超时时间。

References: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetdefaultnavigationtimeouttimeout https://pptr.dev/#?product=Puppeteer&version=v1.17.0&show=api-pagesetdefaultnavigationtimeouttimeout参考资料: https : //github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetdefaultnavigationtimeouttimeout https://pptr.dev/#?product=Puppeteer&version=v1.17.0&show=api-pagesetdefaultnavigationtimeouttimeout

There are two methods to handle the timeouts in Puppeteer:在 Puppeteer 中有两种处理超时的方法:

a) page.setDefaultNavigationTimeout(timeoutInMiliseconds) a) page.setDefaultNavigationTimeout(timeoutInMiliseconds)

It affects the navigation-related functions:它会影响与导航相关的功能:

•   page.goBack([options])
•   page.goForward([options])
•   page.goto(url[, options])
•   page.reload([options])
•   page.setContent(html[, options])
•   page.waitForNavigation([options])

b) page.setDefaultTimeout(timeoutInMiliseconds) b) page.setDefaultTimeout(timeoutInMiliseconds)

It affects all the previous navigation functions plus all the Waiting funtions:它会影响所有以前的导航功能以及所有等待功能:

•   page.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])
•   page.waitForFunction(pageFunction[, options[, ...args]])
•   page.waitForRequest(urlOrPredicate[, options])
•   page.waitForResponse(urlOrPredicate[, options])
•   page.waitForSelector(selector[, options])
•   page.waitForXPath(xpath[, options])

NOTE : page.setDefaultNavigationTimeout takes priority over page.setDefaultTimeout注意page.setDefaultNavigationTimeout优先于page.setDefaultTimeout

You can set timeout like this您可以像这样设置超时

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

networkidle2 comes handy for pages that do long-polling or any other side activity. networkidle2 对于进行长轮询或任何其他辅助活动的页面非常方便。

Check https://github.com/puppeteer/puppeteer/issues/1552#issuecomment-350954419检查https://github.com/puppeteer/puppeteer/issues/1552#issuecomment-350954419

I got same error but not on directly using node.js application.我遇到了同样的错误,但没有直接使用 node.js 应用程序。 I faced this issue when I was using MagePack installed on the Ubuntu server.我在使用安装在 Ubuntu 服务器上的MagePack时遇到了这个问题。

I fixed it by increasing the timeout in the following file /ur/local/lib/node_modules/magepack/node_modules/puppeteer/libTimeoutSettings.js我通过增加以下文件/ur/local/lib/node_modules/magepack/node_modules/puppeteer/libTimeoutSettings.js 中的超时来修复它

const DEFAULT_TIMEOUT = 30000

The default in puppeteer timeout is 30 seconds. puppeteer 超时的默认值为 30 秒。 To use custom timeouts, you can use the setDefaultNavigationTimeout and setDefaultTimeout methods or the timeout property in the options parameter.要使用自定义超时,您可以使用 setDefaultNavigationTimeout 和 setDefaultTimeout 方法或 options 参数中的 timeout 属性。 The wait time in all cases is specified in milliseconds.所有情况下的等待时间都以毫秒为单位指定。

await page.setDefaultNavigationTimeout(60000);

eg例如

const page = await browser.newPage();            
await page.setDefaultNavigationTimeout(60000); //timeout 60 seconds now

Pass 0 to disable the timeout传递 0 以禁用超时

await page.setDefaultNavigationTimeout(0); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM