我想多次截取一个特定的 class
,但每次都会显示 Session Closed
或 Terminated
,因此我将多个屏幕截图实现为在多个实例中打开。
有人能指导如何在同一浏览器实例上使用多个实例吗?
我的代码
:
const puppeteer = require("puppeteer");
const SELECTOR = ".octicon";
(async () => {
let screenshotNumber = 0;
async function screenshots() {
const browser = await puppeteer.launch({
headless: true
});
try {
let page = await browser.newPage();
page.setViewport({ width: 1000, height: 600, deviceScaleFactor: 2 });
await page.goto("https://github.com/");
await page.waitForNavigation({ waitUntil: "networkidle" });
const rect = await page.evaluate(selector => {
const element = document.querySelector(selector);
const { x, y, width, height } = element.getBoundingClientRect();
return { left: x, top: y, width, height, id: element.id };
}, SELECTOR);
await page.screenshot({
path: `octicon-${screenshotNumber}.jpg`,
clip: {
x: rect.left,
y: rect.top,
width: rect.width,
height: rect.height
}
});
browser.close();
screenshotNumber++;
} catch (e) {
console.log(e);
browser.close();
}
}
async function run() {
await screenshots();
setTimeout(run, 200);
}
run();
})();