UnhandledPromiseRejectionWarning:错误:页面崩溃!使用木偶时

Dan*_*ond 10 javascript mocha.js node.js puppeteer

所以我使用了一个 while 循环,所以我的测试将在一个恒定循环中运行,直到我的后端崩溃。我已经实现了一个 try and catch(error) 所以任何前端崩溃自动化都会刷新并继续运行

while(true){
try{
    await page.waitFor(selector)
    await page.click(selector)    
}
catch(error){
    console.log("FE crashed with\n\n" + error + "\n\nRefreshing page and continuing profile switching")
    await page.reload(page);
    continue;
}}
Run Code Online (Sandbox Code Playgroud)

因此,确实任何超时错误都会自动刷新页面并继续循环。但我收到了不同的崩溃错误

(node:6535) UnhandledPromiseRejectionWarning: Error: Page crashed!
at Page._onTargetCrashed (/home/raymond/node_modules/puppeteer/lib/Page.js:170:24)
at CDPSession.Page.client.on.event (/home/raymond/node_modules/puppeteer/lib/Page.js:125:56)
at CDPSession.emit (events.js:182:13)
at CDPSession._onMessage (/home/raymond/node_modules/puppeteer/lib/Connection.js:200:12)
at Connection._onMessage (/home/raymond/node_modules/puppeteer/lib/Connection.js:112:17)
at _tickCallback (internal/process/next_tick.js:43:7)
at listOnTimeout (timers.js:294:7)
at processTimers (timers.js:268:5)
(node:6535) 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)
Run Code Online (Sandbox Code Playgroud)

我该如何处理这个错误?,如果我手动刷新页面一切正常。谢谢

Tho*_*orf 17

您假设错误发生是因为页面导航失败。可能是这种情况,但也可能是不同的错误,例如Protocol error。在这种情况下,您不能仅仅重用page对象,而必须首先重新启动浏览器。

前段时间,为了测试目的,我抓取了大约 40 万个页面。我总共经历了 34 次这些 puppeteer 崩溃,其中一些意外错误导致整个浏览器崩溃。为了强化您的代码以防止此类崩溃,您需要一种可靠的方法来重新启动浏览器。

代码示例

let browser = await puppeteer.launch(/* .. */);
let page = await browser.newPage();

while(true) {
    try {
        // your code that might crash
    } catch (err) {
        try {
            await page.reload(); // soft fix
        } catch (recoveringErr) {
            // unable to reload the page, hard fix
            try {
                await browser.close();
            } catch (err) { // browser close was not necessary
                // you might want to log this error
            }
            browser = await puppeteer.launch(/* .. */);
            page = await browser.newPage();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

尽管此代码包含三个嵌套try..catch块可能看起来很糟糕,但它在保持代码运行方面做得很好。

首先,执行您的原始代码。如果出现最初的问题,page.reload则会尝试通过呼叫来解决问题。如果这有效,循环将继续运行。如果这不起作用,浏览器将重新启动。

要重新启动浏览器,我建议先尝试关闭旧浏览器。尽管这可能会失败,但它会清除所有内存并正确处理浏览器对象。根据您的用例,您可能想要记录错误或简单地忽略它。处理完旧的浏览器对象后,您可以重新启动浏览器,循环可能会继续。

选择

作为替代方案,您可以使用我的库puppeteer-cluster,它内置了错误处理功能。在页面不再可用的情况下,该库会自动重新启动浏览器。此外,该库可以并行运行多个页面(因为您正在尝试对我假设的服务器进行压力测试)。