螢幕截圖
簡介
這裡有一個快速截取螢幕截圖並儲存到檔案的方法
- 同步
- 非同步
page.screenshot(path="screenshot.png")
await page.screenshot(path="screenshot.png")
螢幕截圖 API 接受許多參數,例如圖片格式、剪裁區域、品質等等。請務必查看它們。
完整頁面螢幕截圖
完整頁面螢幕截圖是整個可捲動頁面的螢幕截圖,就像您有一個非常高的螢幕並且頁面可以完全容納它一樣。
- 同步
- 非同步
page.screenshot(path="screenshot.png", full_page=True)
await page.screenshot(path="screenshot.png", full_page=True)
擷取到緩衝區
您可以取得包含圖片的緩衝區,而不是寫入檔案,並對其進行後處理,或將其傳遞給第三方像素差異工具。
- 同步
- 非同步
screenshot_bytes = page.screenshot()
print(base64.b64encode(screenshot_bytes).decode())
# Capture into Image
screenshot_bytes = await page.screenshot()
print(base64.b64encode(screenshot_bytes).decode())
元素螢幕截圖
有時,截取單個元素的螢幕截圖很有用。
- 同步
- 非同步
page.locator(".header").screenshot(path="screenshot.png")
await page.locator(".header").screenshot(path="screenshot.png")