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