简体   繁体   English

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

[英]UnhandledPromiseRejectionWarning: Error: Page crashed! While using puppeteer

So I used a while loop so my test will run in a constant loop until my back-end will crash.所以我使用了一个 while 循环,所以我的测试将在一个恒定循环中运行,直到我的后端崩溃。 I've implemented a try and catch(error) so any front-end crash the automation will refresh and keep running我已经实现了一个 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;
}}

So indeed any timeout error the automation refreshes the page and continuing the loop.因此,确实任何超时错误都会自动刷新页面并继续循环。 but Im recieving a different crash error但我收到了不同的崩溃错误

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

How can I handle this error?, if I refresh the page manually everything works fine.我该如何处理这个错误?,如果我手动刷新页面一切正常。 Thanks谢谢

You are assuming, that the error happened because the page navigation failed.您假设错误发生是因为页面导航失败。 This might be the case, but it could also be a different error like a Protocol error .可能是这种情况,但也可能是不同的错误,例如Protocol error In a case like that, you cannot just reuse the page object, but you first have to restart the browser.在这种情况下,您不能仅仅重用page对象,而必须首先重新启动浏览器。

Some time ago, I crawled roughly 400k pages for testing purposes .前段时间, 为了测试目的,我抓取了大约 40 万个页面 In total, I experienced 34 of these puppeteer crashes, where some unexpected error crashes the whole browser.我总共经历了 34 次这些 puppeteer 崩溃,其中一些意外错误导致整个浏览器崩溃。 To harden your code against these kind of crashes, you need a robust way to restart the browser.为了强化您的代码以防止此类崩溃,您需要一种可靠的方法来重新启动浏览器。

Code Sample代码示例

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

Although this code might look horrendous with three nested try..catch blocks, it does a good job of keeping your code running.尽管这段代码包含三个嵌套的try..catch块可能看起来很try..catch ,但它在保持代码运行方面做得很好。

First, your original code is executed.首先,执行您的原始代码。 If the initial problem happens, a page.reload call is tried to fix the problem.如果出现最初的问题,则会尝试调用page.reload来解决问题。 In case this works, the loop will continue running.如果这有效,循环将继续运行。 If that does not work, the browser will be restarted.如果这不起作用,浏览器将重新启动。

For restarting the browser, I recommend to first try to close the old browser.要重新启动浏览器,我建议先尝试关闭旧浏览器。 Although, this might likely fail, it clears all memory and correctly disposes the browser object.尽管这可能会失败,但它会清除所有内存并正确处理浏览器对象。 Depending on your use case, you might want to log the error or simply ignore it.根据您的用例,您可能想要记录错误或简单地忽略它。 After disposing the old browser object, you can restart the browser and the loop may continue.处理完旧的浏览器对象后,您可以重新启动浏览器,循环可能会继续。

Alternative选择

As an alternative, you could use my library puppeteer-cluster which has error handling build in. The library automatically restarts the browser in cases in which the page is not usable anymore.作为替代方案,您可以使用我的库puppeteer-cluster ,它内置了错误处理功能。在页面不再可用的情况下,该库会自动重新启动浏览器。 In addition, the library can run multiple pages in parallel (as you are trying to stress-test the server I'm assuming).此外,该库可以并行运行多个页面(因为您正在尝试对我假设的服务器进行压力测试)。

As per the official documentation, an 'error' event is emitted when the page crashes which can be used to do certain stuff basis the application.根据官方文档,当页面崩溃时会发出一个 'error' 事件,该事件可用于执行基于应用程序的某些操作。

page.on('error', err => { /*custom logic to handle the crash*/ });

For the above specific use case, you could go about doing something like this:对于上述特定用例,您可以执行以下操作:

 let browser = await puppeteer.launch(); let page = await getNewPage(browser); while(true){ try{ if (page.isClosed()) { page = await getNewPage(browser); } await page.waitFor(selector) await page.click(selector) } catch(error){ console.log("FE error with\\n\\n" + error + "\\n\\nRefreshing page and continuing profile switching") await page.reload(); continue; }} async function getNewPage(browser) { let page = await browser.newPage(); page.on('error', err => { if (!page.isClosed()) { //Close page if not closed already page.close(); } } return page; }

Reference: page.on('error')参考: page.on('error')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 puppeteer 抛出错误“UnhandledPromiseRejectionWarning”并崩溃 - puppeteer throw error “UnhandledPromiseRejectionWarning” and crash Puppeteer 错误:页面崩溃! 试图获取带有完全加载正文的页面 - Puppeteer Error: Page Crashed! trying to get page with fully loaded body Puppeteer UnhandledPromiseRejectionWarning - Puppeteer UnhandledPromiseRejectionWarning 使用 puppeteer 进行 JS 网页抓取,得到这个错误:(node:12121) UnhandledPromiseRejectionWarning: TypeError: src.jsonValue is not a function - JS web scraping using puppeteer, getting this error: (node:12121) UnhandledPromiseRejectionWarning: TypeError: src.jsonValue is not a function 使用 puppeteer 连接访问页面时出现问题 - Problem accessing the page while using puppeteer connection 在保留缓存的同时使用 Puppeteer 重定向页面? - Redirect Page Using Puppeteer While Keeping cache? 使用axios的UnhandledPromiseRejectionWarning错误 - UnhandledPromiseRejectionWarning error using axios UnhandledPromiseRejectionWarning:运行 puppeteer 脚本时未处理的承诺拒绝 - UnhandledPromiseRejectionWarning: Unhandled promise rejection while running puppeteer script 运行Puppeteer时UnhandledPromiseRejectionWarning? - UnhandledPromiseRejectionWarning when running Puppeteer? 如何处理 node.js puppeteer 中的页面崩溃异常? - How to handle page crashed exception in node.js puppeteer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM