Node.js puppeteer - How to set navigation timeout?

孤者浪人 提交于 2020-01-12 11:40:40

问题


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:

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

回答1:


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

Use it in your page.goto like:

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:


UPDATE 2019

You can also change the page behaviour since 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




回答3:


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



回答4:


There are two methods to handle the timeouts in Puppeteer:

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

It affects all the previous Navegation 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



来源:https://stackoverflow.com/questions/52163547/node-js-puppeteer-how-to-set-navigation-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!