スクリーンショット
はじめに
スクリーンショットを撮影してファイルに保存する簡単な方法をご紹介します
- 同期
- 非同期
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")