开始打开网页
2021-12-22T09:32:19.799Z [verbose] 访问 url 次数 1
2021-12-22T09:32:32.340Z [verbose] 标签页 3
2021-12-22T09:33:09.763Z [verbose] 浏览器错误 { Error: Protocol error (Runtime.callFunctionOn): Target closed.
at Promise (/code/node_modules/puppeteer/lib/Connection.js:183:56)
at Promise (null:null:null)
at send (/code/node_modules/puppeteer/lib/Connection.js:182:12)
at _evaluateInternal (/code/node_modules/puppeteer/lib/ExecutionContext.js:107:44)
at evaluateHandle (/code/node_modules/puppeteer/lib/ExecutionContext.js:57:17)
at (/code/node_modules/puppeteer/lib/helper.js:112:23)
at rerun (/code/node_modules/puppeteer/lib/DOMWorld.js:570:65)
at _tickCallback (internal/process/next_tick.js:68:7)
message: 'Protocol error (Runtime.callFunctionOn): Target closed.' }
2021-12-22T09:33:09.764Z [error] (node:23) UnhandledPromiseRejectionWarning: Error: Protocol error (Network.getCookies): Session closed. Most likely the page has been closed.
at send (/code/node_modules/puppeteer/lib/Connection.js:180:29)
at cookies (/code/node_modules/puppeteer/lib/Page.js:382:32)
at (/code/node_modules/puppeteer/lib/helper.js:112:23)
at Promise (/code/aicard-event.js:367:28)
2021-12-22T09:33:09.764Z [error] (node:23) 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: 2)
2021-12-22T09:33:09.764Z [error] (node:23) [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.
FC Invoke End RequestId: , Error: Function timed out after 120 seconds (maxMemoryUsage: 0MB)
我这块在第一次打开页面的时候waitForFunction
报错提示超时,于是我会再重试一次,这个时候waitForFunction
就会报Protocol error (Runtime.callFunctionOn): Target closed.
,这个问题我十次里面会遇到一两次,想问问这个是为啥啊,我中间 url 啥的都没改,就只是重新再 page.goto 一次,为什么会提示这个错误呢。
还有一个问题,这个是我这个方法的结构,我上面waitForFunction
报错之后进入 catch ,在这里面获取 cookie ,但是目标关闭导致失败,这个时候什么会提示后面的错误,我想问问我这个不是写了 reject 吗,完了在调用toPDF
时也写了.then().catch(),请问这个正确的写法该是怎样的呢
function toPDF() {
return new Promise((resolve, reject) => {
for (let i = 0; i < 2; i++) {
try {
//打开页面生成 pdf
resolve()
} catch (e) {
// 获取 cookie
}
}
reject(error)
})
}
1
v2byy 2021-12-22 22:22:50 +08:00
1. target.close 一般是页面已经关闭,比如 page.close.
page.goto 可以指定 waitUntil 参数,为啥非要用 waitForFunction? 如果具体像知道什么错误,可以给 puppeteer 传递 headless 为 false ,可以看到 UI 。 2. 看起来像是在 catch 里面又抛出了异常,但是应该可以在 toPdf().then().catch(), 在 catch 到吧 具体可以用 vscode debug 一下啊 |
2
aglsv OP @v2byy 用 webstorm 看了没问题写法没啥问题,没搞懂为啥。waitUntil 我设置了,用 wiaiForFunction 是为了等待 window.status 为 load ,只是设置网络请求完成的话有可能页面还没渲染完,pqge.close 只在 waitfoefunction 成功后判断有无错误,在 page.pdf 后才会关闭,但是现在第一次超时了并没有走到那一步。
|