简体   繁体   English

Puppeteer:在一个浏览器实例中有多个屏幕截图

[英]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. 所以我想多次截取一个特定的class ,但是它总是会说Session ClosedTerminated ,因此我开始实现打开多个实例的多个屏幕截图。

Could someone at least guide how to multiple instances on the same browser instance? 有人至少可以指导如何在同一个浏览器实例上运行多个实例吗?

my code : 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: 只需连续运行两个屏幕截图命令而无需调用browser.close()可以正常工作:

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

暂无
暂无

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

相关问题 使用 Puppeteer 迭代多个有效负载并截取多个屏幕截图 AWS Lambda - Iterate over multiple payloads and take multiple screenshots with Puppeteer AWS Lambda 如何使用 Puppeteer 自动截取多个页面的屏幕截图 - How to take screenshots for multiple pages automatically using Puppeteer 多个单独的浏览器,每个浏览器都有一个选项卡 - 与页面上的元素同时交互(木偶无头) - Multiple separate browser with one tab each - simultaneous interaction with elements on pages (puppeteer headless) Puppeteer - Cucumber - 使用一个浏览器进行测试 - Puppeteer - Cucumber - Use one browser for the tests Puppeteer 使用单个浏览器实例运行所有测试文件 - Puppeteer Run All Test Files With Single Browser Instance 始终打开 puppeteer 浏览器作为全局实例与为每个请求使用新的浏览器实例 - having puppeteer browser as global instance open at all time vs using new browser instance for each request 在 Puppeteer 中截取具有特定名称的不同元素的屏幕截图 - Take screenshots of different elements with specific names in Puppeteer Puppeteer + Pixelmatch:比较 Mac 和 TravisCI Linux 中的屏幕截图 - Puppeteer + Pixelmatch: Comparing screenshots in Mac and TravisCI Linux 如何处理 puppeteer-cluster[CONCURRENCY_BROWSER] 中的多个选项卡? - How to handle multiple tabs in puppeteer-cluster[CONCURRENCY_BROWSER]? FFMPEG 多次截图命令 - FFMPEG multiple screenshots command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM