Taking Screenshots and PDFs with Playwright
Learn to capture full-page screenshots, element screenshots, and generate PDFs from web pages using Playwright.
Browser Automation · #6beginner2 min read
Capturing screenshots and PDFs is essential for visual verification, archiving web pages, monitoring changes, and debugging scrapers. Playwright makes this straightforward with built-in methods that support full-page captures, element-specific screenshots, and high-quality PDF generation.
Full-Page Screenshots
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://quotes.toscrape.com")
page.wait_for_load_state("networkidle")
# Screenshot of the visible viewport
page.screenshot(path="viewport.png")
# Full-page screenshot (scrolls and captures everything)
page.screenshot(path="fullpage.png", full_page=True)
browser.close()
Element Screenshots
Capture just a specific element instead of the entire page:
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://quotes.toscrape.com")
# Screenshot a single quote
quote = page.query_selector(".quote")
quote.screenshot(path="single_quote.png")
browser.close()
Screenshot Options
page.screenshot(
path="capture.png",
full_page=True,
type="png", # "png" or "jpeg"
quality=80, # JPEG quality (1-100), ignored for PNG
omit_background=True, # Transparent background for PNG
)
# Get screenshot as bytes (useful for saving to S3 or database)
screenshot_bytes = page.screenshot(full_page=True)
Custom Viewport Size
Control the browser window size before capturing:
page = browser.new_page(viewport={"width": 1920, "height": 1080})
page.goto("https://example.com")
page.screenshot(path="desktop.png")
# Mobile viewport
mobile_page = browser.new_page(viewport={"width": 375, "height": 812})
mobile_page.goto("https://example.com")
mobile_page.screenshot(path="mobile.png")
Generating PDFs
PDF generation works only with Chromium in headless mode:
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://quotes.toscrape.com")
page.wait_for_load_state("networkidle")
page.pdf(
path="page.pdf",
format="A4",
print_background=True,
margin={
"top": "1cm",
"bottom": "1cm",
"left": "1cm",
"right": "1cm"
}
)
browser.close()
Practical Use Case: Visual Change Monitoring
import hashlib
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://example.com/pricing")
page.wait_for_load_state("networkidle")
screenshot = page.screenshot(full_page=True)
current_hash = hashlib.md5(screenshot).hexdigest()
# Compare with previously stored hash to detect changes
print(f"Page hash: {current_hash}")
browser.close()
Next Steps
- Scrape infinite scroll pages with Playwright
- Handle forms, dropdowns, and click interactions
- Use Playwright with proxies for large-scale captures