Playwright Screenshot Capture Error Fix
In this tutorial, you'll learn about Playwright Screenshot Capture Error Fix. We cover key concepts, practical examples, and best practices.
Your Playwright screenshot fails with Error: Page content exceeds viewport or returns a blank image — the page hasn't finished loading, the viewport is too small, or the element isn't rendered yet.
The Problem
# WRONG — screenshot before page finishes loading
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto('https://example.com')
page.screenshot(path='screenshot.png')
Produces a white or partial screenshot because the page hasn't finished loading and rendering all elements, especially JavaScript-rendered content.
Step-by-Step Fix
1. Wait for network idle
page.goto('https://example.com', wait_until='networkidle')
page.screenshot(path='screenshot.png', full_page=True)
networkidle waits until there are no network connections for 500ms. This ensures all API calls, images, and script loads complete.
2. Capture full-page scrollable screenshots
page.screenshot(path='full-page.png', full_page=True)
Without full_page=True, Playwright captures only the visible viewport. With full_page=True, it scrolls through the entire page and stitches a composite image.
3. Capture a specific element
element = page.get_by_test_id('chart-container')
element.screenshot(path='chart.png')
This captures exactly the element's bounding box — useful for comparing charts, tables, or modals in snapshot tests.
4. Set viewport size for consistent screenshots
page.set_viewport_size({'width': 1280, 'height': 720})
page.goto('https://example.com', wait_until='networkidle')
page.screenshot(path='desktop.png')
page.set_viewport_size({'width': 375, 'height': 812})
page.goto('https://example.com', wait_until='networkidle')
page.screenshot(path='mobile.png')
5. Handle animations and transitions
# Disable CSS animations for consistent screenshots
page.add_style_tag(content='* { animation: none !important; transition: none !important; }')
page.screenshot(path='no-animations.png')
Expected output:
✓ Desktop screenshot saved: desktop.png (1280x720)
✓ Mobile screenshot saved: mobile.png (375x812)
✓ Chart element screenshot saved: chart.png
Prevention Tips
- Always wait for
networkidlebefore taking screenshots - Use
full_page=Truefor scrollable content - Capture elements directly with
element.screenshot() - Set a fixed viewport size for consistent results across runs
- Disable animations for visual regression testing
Common Mistakes with screenshot error
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro