简体   繁体   English

傀儡师黑色截图

[英]Puppeteer black screenshot

When I make a screenshot with the x , y , width and height parameters, puppeteer returns a black screenshot, without it all works fine.当我使用xywidthheight参数制作屏幕截图时,puppeteer 返回一个黑色屏幕截图,没有它一切正常。 This is my code:这是我的代码:

await page.screenshot({ path: 'build.png', clip: { x: 0, y: 0, width: 810, height: 415 } });

Is there any way to fix that?有没有办法解决这个问题?

For me your snippet works as expected (Windows 10, puppeteer 3.1.0).对我来说,您的代码片段按预期工作(Windows 10,puppeteer 3.1.0)。 It may be an environment or webpage dependent issue (Eg docs say for OSX screenshots take at least 1/6 second).这可能是环境或网页相关问题(例如, 文档说OSX 屏幕截图至少需要 1/6 秒)。

As a workaround you can use page.setViewport to crop the desired size, then do a normal screenshot.作为一种解决方法,您可以使用page.setViewport裁剪所需的大小,然后进行正常的屏幕截图。

The images build.png and build2.png are 100% identical to me (size, crop, everything) so if the only problem for you is clip then you can go on with this solution.图像build.pngbuild2.png与我 100% 相同(大小、裁剪、所有内容),所以如果您的唯一问题是clip ,那么您可以使用此解决方案进行 go。

const puppeteer = require('puppeteer')

async function fn() {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  await page.setViewport({ width: 810, height: 415 })
  await page.goto('https://www.google.com')
  await page.screenshot({ path: 'build.png', clip: { x: 0, y: 0, width: 810, height: 415 } }) // your original try
  await page.screenshot({ path: 'build2.png' }) // workaround, combined with setViewport

  await browser.close()
}
fn()

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

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