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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52163547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:57:00  来源:igfitidea点击:

Node.js puppeteer - How to set navigation timeout?

javascriptnode.jspuppeteer

提问by Philipp M

I'm using node.js and puppeteer to get some data. Some of the files I'm opening are quite large ... and then I get an error:

我正在使用 node.js 和 puppeteer 来获取一些数据。我打开的一些文件非常大......然后我收到一个错误:

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'});

回答by Jay Gould

You can use timeout: 0to disabled timeout errors if you're loading a heavy page.

timeout: 0如果您正在加载大量页面,您可以使用禁用超时错误。

Use it in your page.gotolike:

在你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,其中添加了更改,以及实现它的文档和单元测试。

回答by Juanmabs22

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#pagesetdefaultnavigationtimeouttimeouthttps://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

回答by Juanma Menendez

There are two methods to handle the timeouts in Puppeteer:

在 Puppeteer 中有两种处理超时的方法:

a)page.setDefaultNavigationTimeout(timeoutInMiliseconds)

一种)page.setDefaultNavigationTimeout(timeoutInMiliseconds)

It affects the Navegation 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 Navegation functions plus all the Waiting funtions:

它会影响所有以前的 Navegation 函数以及所有 Waiting 函数:

?   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.setDefaultNavigationTimeouttakes priority over page.setDefaultTimeout

注意page.setDefaultNavigationTimeout优先于page.setDefaultTimeout

回答by Henry

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)
})

回答by Rexben

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