How to take a screenshot with Playwright?

You can take a screenshot in Playwright with the page.screenshot() method. Use the path argument to choose where Playwright saves the image.

Before running the examples, install Playwright and the Chromium browser:

python3 -m pip install playwright
python3 -m playwright install chromium

Take a Screenshot with Playwright

Create a file named screenshot.py and add the following code:

from playwright.sync_api import sync_playwright

with sync_playwright() as playwright:
    browser = playwright.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto("https://www.scrapingbee.com/")
    page.screenshot(path="screenshot.png")
    browser.close()

Run the script:

python3 screenshot.py

The script creates screenshot.png, containing the visible portion of the ScrapingBee homepage:

Screenshot of the ScrapingBee homepage saved by Playwright with page.screenshot()

Playwright saves the screenshot in the current working directory unless you provide another path.

You can confirm that the file was created with:

ls screenshot.png

Output:

screenshot.png

Take a Full-Page Screenshot

By default, Playwright captures the visible viewport. To capture the entire page, set full_page=True:

page.screenshot(path="full-page.png", full_page=True)

This creates full-page.png, containing the entire page instead of only the visible viewport.

Related Playwright web scraping questions: