gpt4 book ai didi

javascript - puppeteer 师 : how check if browser is still open and working

转载 作者:行者123 更新时间:2023-12-04 14:54:11 30 4
gpt4 key购买 nike

我试图在单个浏览器实例中打开多个选项卡,完成后我关闭选项卡,然后每隔 x 秒重新打开这些选项卡......但我想保持浏览器本身打开,所以我不必登录每个循环上的每个选项卡

所以浏览器保持打开,标签打开和关闭

这是我的简化代码,请忽略语法错误

var  global_browser = false ;
async function init_puppeteer( settings ) {



if(global_browser === false )
global_browser = await puppeteer.launch({headless: false , args:['--no-sandbox']});

for(var i = 0 ; i < settings.length ; i++ )
{
var setting = settings[i];
open_tab($setting);
}
}



async function open_tab( setting ){
const page = await global_browser.newPage();
// do stuff
page.close();
}

setInterval(function (){
init_puppeteer(settings)
}, 50000 );

这就是问题所在,有时浏览器会崩溃或出于任何原因关闭,但 global_browser仍然是 puppeteer 的对象/实例......当我尝试使用该实例打开选项卡时它不会工作,我得到类似的东西
(node:5720) UnhandledPromiseRejectionWarning: Error: WebSocket is not open: readyState 3 (CLOSED)

这是我的问题,我如何检查并确保我在 global_browser 中有一个工作/打开的 puppeteer 实例?所以我可以创建一个新实例并在前一个实例关闭时替换它

最佳答案

使用browser.on('disconnected') .
您可以使用 browser.on('disconnected') 监听浏览器何时关闭或崩溃,或者 browser.disconnect() 方法被调用。
然后,您可以自动重新启动浏览器,并继续您的程序。
以下类将启动浏览器,并在 'disconnected' 时自动重新启动浏览器。事件被触发:

class BrowserHandler {
constructor() {
const launch_browser = async () => {
this.browser = false;
this.browser = await puppeteer.launch();
this.browser.on('disconnected', launch_browser);
};

(async () => {
await launch_browser();
})();
}
}
BrowserHandler.browser将返回 false断开连接时。
此外,以下功能将使您能够强制程序等待浏览器完成启动,然后再继续:
const wait_for_browser = browser_handler => new Promise((resolve, reject) => {
const browser_check = setInterval(() => {
if (browser_handler.browser !== false) {
clearInterval(browser_check);
resolve(true);
}
}, 100);
});

完整示例:
'use strict';

const puppeteer = require('puppeteer');

/**
* Class: BrowserHandler
* Relaunch Browser when Closed
* Return false when Browser is Disconnected
*/

class BrowserHandler {
constructor() {
const launch_browser = async () => {
this.browser = false;
this.browser = await puppeteer.launch();
this.browser.on('disconnected', launch_browser);
};

(async () => {
await launch_browser();
})();
}
}

/**
* Function: wait_for_browser
* Wait for Browser to Finish Launching
*/

const wait_for_browser = browser_handler => new Promise((resolve, reject) => {
const browser_check = setInterval(() => {
if (browser_handler.browser !== false) {
clearInterval(browser_check);
resolve(true);
}
}, 100 );
});

// Main Program

(async () => {
// Open Browser

const browser_handler = new BrowserHandler;

await wait_for_browser(browser_handler);

// Open New Page

let page = await browser_handler.browser.newPage();

await page.goto('https://www.example.com/');

// Disconnect and Automatically Reconnect Browser

browser_handler.browser.disconnect();

if (browser_handler.browser === false) {
console.log('The browser has been disconnected.');

await wait_for_browser(browser_handler);
}

console.log('The browser has been reconnected.');

// Close and Automatically Reopen Browser

await browser_handler.browser.close();

if (browser_handler.browser === false) {
console.log('The browser has been closed.');

await wait_for_browser(browser_handler);
}

console.log('The browser has been reopened.');

// Force Close Puppeteer

process.exit();
})();
结果:

The browser has been disconnected.

The browser has been reconnected.

The browser has been closed.

The browser has been reopened.

关于javascript - puppeteer 师 : how check if browser is still open and working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045600/

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