Skip to main content

截图

介绍

🌐 Introduction

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

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

page.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get("screenshot.png")));

Screenshots API 接受许多用于图片格式、剪切区域、质量等的参数。请务必查看它们。

整页截图

🌐 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.

page.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get("screenshot.png"))
.setFullPage(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.

byte[] buffer = page.screenshot();
System.out.println(Base64.getEncoder().encodeToString(buffer));

元素截图

🌐 Element screenshot

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

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

page.locator(".header").screenshot(new Locator.ScreenshotOptions().setPath(Paths.get("screenshot.png")));