Skip to main content

截图

介绍

Introduction

以下是捕获屏幕截图并将其保存到文件中的快速方法:

Here is a quick way to capture a screenshot and save it into a file:

await page.screenshot({ path: 'screenshot.png' });

Screenshots API 接受许多图片格式、剪辑区域、质量等参数。请务必检查它们。

Screenshots API accepts many parameters for image format, clip area, quality, etc. Make sure to check them out.

整页截图

Full page screenshots

全页屏幕截图是完整可滚动页面的屏幕截图,就好像你有一个非常高的屏幕并且页面可以完全容纳它一样。

Full page screenshot is a screenshot of a full scrollable page, as if you had a very tall screen and the page could fit it entirely.

await page.screenshot({ path: 'screenshot.png', fullPage: true });

捕获到缓冲区

Capture into buffer

你可以获取包含图片的缓冲区并对其进行后处理或将其传递给第三方像素差异工具,而不是写入文件。

Rather than writing into a file, you can get a buffer with the image and post-process it or pass it to a third party pixel diff facility.

const buffer = await page.screenshot();
console.log(buffer.toString('base64'));

元素截图

Element screenshot

有时,截取单个元素的屏幕截图很有用。

Sometimes it is useful to take a screenshot of a single element.

await page.locator('.header').screenshot({ path: 'screenshot.png' });