V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
aglsv
V2EX  ›  Node.js

puppeteer 加载网页失败

  •  
  •   aglsv · 2021-12-22 18:35:59 +08:00 · 5884 次点击
    这是一个创建于 826 天前的主题,其中的信息可能已经有所发展或是发生改变。
    开始打开网页
    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)
        })
    }
    
    
    5 条回复    2021-12-23 11:52:48 +08:00
    v2byy
        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 一下啊
    aglsv
        2
    aglsv  
    OP
       2021-12-23 10:07:54 +08:00 via Android
    @v2byy 用 webstorm 看了没问题写法没啥问题,没搞懂为啥。waitUntil 我设置了,用 wiaiForFunction 是为了等待 window.status 为 load ,只是设置网络请求完成的话有可能页面还没渲染完,pqge.close 只在 waitfoefunction 成功后判断有无错误,在 page.pdf 后才会关闭,但是现在第一次超时了并没有走到那一步。
    aglsv
        3
    aglsv  
    OP
       2021-12-23 10:11:40 +08:00
    @v2byy 只用 waitUntil 设置为 load 的话还要写一个计时器来判断是否超时,所以我直接用了 waitForFunction 来设置
    v2byy
        4
    v2byy  
       2021-12-23 11:11:18 +08:00
    @aglsv page.goto 支持设置 timeout 参数啊
    aglsv
        5
    aglsv  
    OP
       2021-12-23 11:52:48 +08:00
    @v2byy 他那个不是跳转等待时间吗,我还以为是跳转到这个链接需要等待的时间
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3887 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:31 · PVG 18:31 · LAX 03:31 · JFK 06:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.