スクリーンショット
はじめに
スクリーンショットを素早くキャプチャしてファイルに保存する方法です。
- 同期
- 非同期
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")