gpt4 book ai didi

puppeteer - Chrome puppeteer 在错误事件上关闭页面

转载 作者:行者123 更新时间:2023-12-02 03:00:02 25 4
gpt4 key购买 nike

我想在 puppeteer 遇到任何错误时关闭页面,有时页面我尝试加载的页面崩溃并且它不调用 .close();

(async () => {
const page = await browser.newPage();
await page.setViewport({width: resWidth, height: resHeight});
await page.goto(d["entities"]["urls"][0]["expanded_url"], {timeout :90000});
await page.screenshot({path: './resimdata/'+d['id']+'.png' ,fullPage: true});
await page.close();
})();

最佳答案

puppeteer repo 上有一个关于此的问题/PR,这在类似情况下会有所帮助。

相关问题链接:https://github.com/GoogleChrome/puppeteer/issues/952

同时,你可以试试这个小技巧,如果0.12+版本有PR,我们就不用担心下面的代码了。

(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();

function handleClose(msg){
console.log(msg);
page.close();
browser.close();
process.exit(1);
}

process.on("uncaughtException", () => {
handleClose(`I crashed`);
});

process.on("unhandledRejection", () => {
handleClose(`I was rejected`);
});

await page.goto("chrome://crash");

})();

这将输出如下内容,
▶ node app/app.js
I was rejected

关于puppeteer - Chrome puppeteer 在错误事件上关闭页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46673666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com