简体   繁体   中英

Puppeteer: multiple screenshots in one browser instance

so I want to screenshot a specific class multiple times, but all the time it would say Session Closed or Terminated , therefore I worked around implementing multiple screenshots opening multiple instances.

Could someone at least guide how to multiple instances on the same browser instance?

my code :

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

Just running two screenshot commands consecutively without calling browser.close() works fine:

  await page.screenshot({
    path: `octicon-${screenshotNumber}.jpg`,
    clip: {
      x: rect.left,
      y: rect.top,
      width: rect.width,
      height: rect.height
    }
  });

  screenshotNumber++;

  await page.screenshot({
    path: `octicon-${screenshotNumber}.jpg`,
    clip: {
      x: rect.left,
      y: rect.top,
      width: rect.width,
      height: rect.height
    }
  });

  browser.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM