Skip to main content

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")));