有什么方法可以使用puppeteer截屏并将其自动显示在html页面上,而不是将其保存到我的计算机上吗?

问题描述

我正在考虑将pupeteer用于我正在从事的项目,但是我似乎无法弄清楚是否可以拍摄用puppeteer拍摄的屏幕截图并将其自动显示为html页面上的图像。我通读了文档,但没有看到与此相关的任何内容

谢谢!

解决方法

您可以使用base64选项以encoding字符串的形式获取图像:

const screenshot = await page.screenshot({ encoding: 'base64' });

然后,您可以将其作为HTML加载为base64图像:

var html = `<img src="data:image/png;base64,${screenshot}" />`