スクリーンショット
概要
スクリーンショットをキャプチャしてファイルに保存する簡単な方法を紹介します
- 同期
- 非同期
page.screenshot(path="screenshot.png")
await page.screenshot(path="screenshot.png")
Screenshots 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")