ScreenshotNeo

BlogComparisons

Compare Selenium and Playwright for Website Screenshots in Python

Both Selenium and Playwright capture webpage screenshots in Python. Compare viewport, full-page, and element capture, then choose based on your browser and testing needs.

By the ScreenshotNeo team4 October 20269 min read

Short answer: Both Selenium and Playwright can save a screenshot of the visible webpage and return image bytes. For a full scrollable page, Playwright Python documents page.screenshot(path="page.png", full_page=True) directly. Selenium’s standard screenshot methods capture the current window; the Selenium Python API also documents full-document screenshot methods for Firefox, so check the browser and Selenium version before depending on those. Both support element screenshots. Choose based on your browser coverage, existing automation stack, and whether you need Playwright Test’s built-in visual baseline assertions.

1. What each library captures

These APIs capture the rendered webpage inside the browser viewport or document. They do not include the browser application’s tabs, address bar, or window frame. Capturing browser chrome or the whole desktop requires a different screen-capture approach.

Need Playwright Python Selenium Python
Visible viewport page.screenshot(path="page.png") driver.save_screenshot("page.png"), get_screenshot_as_file(), or get_screenshot_as_png()
Full scrollable document page.screenshot(path="page.png", full_page=True) Firefox documents get_full_page_screenshot_as_file() and related methods; confirm support for your installed browser and Selenium version.
One element page.locator(".card").screenshot(path="card.png") driver.find_element(...).screenshot("card.png")
Image bytes page.screenshot() returns bytes driver.get_screenshot_as_png() returns bytes
Visual baseline assertions Playwright Test documents screenshot generation and comparison with toHaveScreenshot(). The reviewed Selenium API documentation does not establish an equivalent first-party baseline workflow. Third-party approaches may exist.

The Playwright Python screenshot guide demonstrates viewport, full-page, buffer, and locator screenshots. The Selenium Python API documents current-window and element screenshots, while its Firefox API documents full-document methods. See the Playwright Python screenshot guide, Selenium WebDriver Python API, and Selenium Firefox WebDriver API.

2. Choose by the job you need to do

  • Choose Playwright when you want a documented Python full-page option, locator-based screenshots, or the Playwright Test visual comparison workflow. It offers synchronous and asynchronous Python APIs; use the style that fits the rest of your program.
  • Choose Selenium when your project already uses Selenium or its browser coverage and automation setup meet your requirements. Its viewport and element screenshot methods are straightforward; treat full-document capture as browser- and version-specific.
  • Keep your existing stack when screenshot capture is only one part of a mature test suite and migrating would add unnecessary maintenance.

The sources reviewed do not establish a universal speed or image-quality winner. Compare the actual browser, page, capture scope, and environment your workflow requires instead of assuming one library is always faster or more accurate.

3. Playwright Python: runnable screenshot examples

Install and capture a viewport or full page

Install Playwright and its Chromium browser, then save this as capture_playwright.py. The script uses the synchronous API, navigates to a page, waits for the page load event, and saves both viewport and full-page PNGs.

python -m pip install playwright
python -m playwright install chromium
from pathlib import Path
from playwright.sync_api import sync_playwright

URL = "https://example.com"

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page(viewport={"width": 1440, "height": 900})
    page.goto(URL, wait_until="load", timeout=60_000)

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

    browser.close()

Run it with python capture_playwright.py. The viewport dimensions are CSS pixels. full_page=True expands the screenshot to the full scrollable document; it does not mean capturing browser chrome.

Capture one element or get bytes

from pathlib import Path
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", wait_until="load", timeout=60_000)

    card = page.locator("main")
    card.screenshot(path="main.png")

    png_bytes = page.screenshot()
    Path("from-bytes.png").write_bytes(png_bytes)
    browser.close()

A locator screenshot is clipped to the matched element. Playwright scrolls it into view and waits for actionability checks; a detached element can cause the capture to fail. If the matched element is a scrollable container, only its currently scrolled content is included. See the locator screenshot API for options such as animation handling, scale, type, quality, style, and timeout.

Async variant

Use the async API if the surrounding application already uses asyncio. Do not call asyncio.run() from inside an event loop that is already running.

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page(viewport={"width": 1440, "height": 900})
        await page.goto("https://example.com", wait_until="load", timeout=60_000)
        await page.screenshot(path="full-page.png", full_page=True)
        await browser.close()

asyncio.run(main())

4. Selenium Python: runnable screenshot examples

Install and capture viewport and element screenshots

Install Selenium, save this as capture_selenium.py, and run it with Python. Selenium Manager can manage drivers for supported browser setups; if your environment uses a separately managed driver, configure that as your project requires.

python -m pip install selenium
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--window-size=1440,900")

driver = webdriver.Chrome(options=options)
try:
    driver.get("https://example.com")

    driver.save_screenshot("viewport.png")
    png_bytes = driver.get_screenshot_as_png()
    Path("from-bytes.png").write_bytes(png_bytes)

    main = driver.find_element(By.CSS_SELECTOR, "main")
    main.screenshot("main.png")
finally:
    driver.quit()

save_screenshot() returns a success boolean. Check it if your code must handle file-write failure explicitly. get_screenshot_as_png() is useful when the next step consumes bytes, such as an image processor or diff tool. The element API captures the selected element.

Full-document screenshot with Firefox

The current Selenium Firefox Python API documents full-document screenshot methods. This is Firefox-specific guidance: check that the exact method exists in your installed Selenium version and use Firefox for this path.

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.add_argument("-headless")
driver = webdriver.Firefox(options=options)
try:
    driver.get("https://example.com")
    saved = driver.get_full_page_screenshot_as_file("full-page.png")
    if not saved:
        raise OSError("Could not save full-page screenshot")
finally:
    driver.quit()

For an existing Selenium installation, check the API reference and installed package before adopting this method. Do not assume that a Firefox full-document method is a portable Selenium API for Chrome or other browsers.

5. Make captures repeatable

  1. Wait for the content you need. A navigation event does not guarantee that client-rendered data, images, or fonts are ready. Wait for a meaningful selector or application state when the page loads content asynchronously.
  2. Control the viewport. Set the same width and height for each capture. Responsive layouts can change at breakpoints.
  3. Stabilize changing content. Use test data, disable animations where appropriate, or hide dynamic regions in your comparison workflow. Playwright locator screenshots support options for animations and injected styles.
  4. Pin the rendering environment for baselines. Playwright warns that output can vary with host OS, browser version, settings, hardware, power source, and headless mode. Generate and compare baselines in the same environment. Read the Playwright visual comparisons guide.
  5. Decide how to handle lazy content. Full-page capture expands the page, but sites may load images only after scrolling or interaction. Scroll through the page or wait for images and other required assets before capture when the initial render leaves gaps.

6. Screenshot options that matter

Requirement Playwright approach Selenium approach
Viewport size Set the page or context viewport when creating it. Set browser window size through options or window management; verify the resulting viewport in your target environment.
Full document full_page=True on page.screenshot(). Use the documented Firefox full-page methods only when supported by the installed browser and Selenium version.
Element Locator .screenshot(); returns bytes and can save by path. Web element .screenshot(filename).
Bytes for processing Omit the path; the call returns bytes. Use get_screenshot_as_png().
Image format and scale Screenshot APIs accept format-related options; consult the API for supported formats, quality, and scale in your installed version. The documented ordinary screenshot methods return or save PNG.
Visual regression Playwright Test offers screenshot baselines and toHaveScreenshot(); this is a test-runner feature. Use your existing assertion or image comparison tooling if required; the reviewed API source does not document Selenium’s own baseline workflow.

7. Troubleshooting

  • Screenshot is blank or incomplete: the page may still be rendering or fetching data. Wait for a selector that represents ready content; inspect network-dependent behavior and authentication.
  • Full-page image stops early: check whether the chosen API supports full-document capture for that browser. Playwright’s Python option is full_page=True; Selenium’s documented full-page methods here are Firefox-specific.
  • Element screenshot throws or misses content: confirm the selector matches one attached element and that the element is visible. For scrollable containers, element capture includes only the currently visible scrolled portion.
  • Screenshot differs in CI: align operating system, browser version, fonts, viewport, headless settings, and test data with the baseline environment. Dynamic content and animations can also produce differences.
  • Browser or driver will not start: install the browser required by Playwright, or check Selenium’s browser and driver setup for the target environment. Confirm headless configuration and permissions in the CI container.
  • File is missing despite a successful navigation: use an absolute or known working output path, ensure the process can write there, and inspect Selenium’s boolean return value for save_screenshot().
  • Expected URL bar is absent: these are webpage screenshots, not desktop-window captures. Use a screen or desktop capture method if browser chrome is part of the requirement.

8. Performance, reliability, and cost

Both approaches run a browser, so browser startup, navigation, page scripts, network activity, and image encoding contribute to end-to-end time. Reuse a browser process when capturing many pages if your application can safely isolate page or context state; close pages and browsers cleanly to avoid leaking resources. No controlled Selenium-versus-Playwright benchmark is established by the sources used here, so measure your own URLs and browser setup.

For reliability, use explicit readiness conditions instead of arbitrary long sleeps, set timeouts deliberately, and capture failures with enough context to reproduce them. Visual baselines are sensitive to the rendering environment; keep CI consistent. Both libraries are software dependencies and require maintaining the browser automation setup. The free open-source libraries do not charge per screenshot; infrastructure, browser execution, and engineering time still have costs.

9. Or skip the browser setup

If you need screenshot files without installing and maintaining a browser automation stack, ScreenshotNeo is the screenshot API to try first: its clean-shot flow removes known consent banners, popups, and chat widgets before capture, and only clean shots are billed. Bot checks, blank pages, failed loads, and cache hits cost nothing; response headers report the page verdict and billing status. It also has an MCP server for AI agents and 1,000 free screenshots a month with no card; paid plans start at $5 for 3,000 shots. See the ScreenshotNeo API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://example.com"},
    timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await Bun.write('shot.webp', res);

Use your API key in place of YOUR_API_KEY. The Python example checks the HTTP response before saving; the Node example uses Bun’s file writer. If you use Node.js without Bun, write the response body with your preferred file API. Sign up for 1,000 free screenshots a month, with no card required.

10. Frequently asked questions

Which should I choose for a Python screenshot script?

Choose the library that fits your existing stack and browser requirements. If full-page capture through a documented cross-context Python API is central, Playwright’s full_page=True is a direct option.

Can Selenium capture a full page in Chrome?

The Selenium source verified here documents full-document methods for Firefox. Check the API for your installed Selenium version and browser before relying on a full-page method in Chrome.

Can I compare screenshots in Playwright without Playwright Test?

The screenshot methods can return bytes for processing, but the documented toHaveScreenshot() baseline workflow belongs to Playwright Test.

Can these APIs capture the browser address bar?

No. They capture webpage content. Use desktop or screen capture tooling to include browser chrome.