Skip to content

How to Take a Screenshot with Playwright

Playwright can capture a page image through page.screenshot().

OUTPUT FORMATSHTMLJSONMarkdown

The path argument controls where that image is written, while the full_page option decides whether the capture covers only what is visible or the entire page.

Set up and run the basic example

Install Playwright and Chromium before running the examples. The protected setup module code0001 contains the exact installation commands. Then create screenshot.py using the protected example module code0002, which launches Chromium, opens a page, calls page.screenshot(path="screenshot.png"), and closes the browser. Run the file with the protected module code0003.

Install Playwright and its browser binaries, then set TARGET_URL to the page you want to capture. The path option selects the output file.

import os
from playwright.sync_api import sync_playwright

with sync_playwright() as engine:
    browser = engine.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto(os.environ["TARGET_URL"])
    page.screenshot(path="screenshot.png")
    browser.close()

Check the saved image

After the run, screenshot.png should exist in the current working directory. Confirm it with the protected listing module code0004; the expected output shown in code0005 is the filename itself. The captured image shows the visible portion of the page that was opened, not content below the fold.

Capture the entire page

Without full_page, Playwright captures the visible viewport. To include the whole page, add full_page=True, as shown in the protected module code0006. That variant writes full-page.png and includes content that requires scrolling, making it better for reviewing long pages or preserving a complete visual record.

Install Playwright and its browser binaries, then set TARGET_URL to the page you want to capture. The path option selects the output file.

import os
from playwright.sync_api import sync_playwright

with sync_playwright() as engine:
    browser = engine.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto(os.environ["TARGET_URL"])
    page.screenshot(path="full-page.png", full_page=True)
    browser.close()

Choose the right capture mode

Viewport capture: choose this when you need exactly what a user sees on screen, a smaller image, or a quick visual check. Full-page capture: choose this when layout, ordering, or content below the fold must be reviewed. In both cases, set path deliberately so the output name and folder are predictable instead of relying on the current working directory.

Pre-run and post-run checklist

Before you run: install Playwright and Chromium; create screenshot.py; open the intended page; set the screenshot path. After you run: list the output file; open the image; check whether it shows only the viewport or the full page; rename or move it if the working directory is not where you want the file stored.

Viewport versus full-page capture
OptionWhat it capturesBest for
No full_page settingVisible viewportQuick visual checks and on-screen review
full_page=TrueEntire pageLong pages and below-the-fold content

Default behavior is viewport-only; full_page=True changes the capture to the entire page.

Frequently asked questions

Where does Playwright save a screenshot by default?

Use the path argument on page.screenshot() to set the filename or location. If no other path is supplied, the image is saved in the current working directory.

Should I capture only the viewport or the full page?

Leave full_page unset for the visible viewport, or set full_page=True when the whole page height is needed. The two options produce different captures, so choose based on whether content below the fold matters.