跳至主要内容

Screenshots

简介

以下是一个快速截取屏幕截图并保存到文件的方法:

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

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

整页截图

全页截图是指对整个可滚动页面的截图,就像你有一个非常高的屏幕,页面可以完全适应它。

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

捕获到缓冲区

除了写入文件外,您还可以获取包含图像的缓冲区并进行后处理,或将其传递给第三方像素差异工具。

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

元素截图

有时对单个元素进行截图很有用。

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