Skip to content

Playwright Screenshot Capture Error Fix

DodaTech Updated 2026-06-24 2 min read

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 networkidle before taking screenshots
  • Use full_page=True for 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

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to 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

### Why is my screenshot blank or showing only a white page?

The page hasn't finished rendering. Use page.wait_for_load_state('networkidle') or wait for a specific element with expect(page.get_by_text('Loaded')).to_be_visible(). Also check if the URL is correct and the page actually loads content.

How do I take screenshots of specific viewport sizes for responsive testing?

Use page.set_viewport_size({'width': W, 'height': H}) before navigating. Playwright renders the page at the specified dimensions. Take multiple screenshots at different breakpoints for responsive visual regression testing. Pair with full_page=True to capture all content at each breakpoint.

Can I use screenshots for visual regression testing in Playwright?

Yes. Playwright integrates with @<a href="/testing-qa/playwright/">playwright</a>/test which supports expect(page).toHaveScreenshot('name.png'). This compares screenshots pixel-by-pixel against stored baselines. Use --update-snapshots to update baselines when UI changes intentionally.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro