Browser screenshots from Selenium preserve the page state that WebDriver saw during a failed test, visual regression check, or documentation handoff. Saving the image from the same script that opens the page keeps the evidence tied to the tested URL, browser window size, and automation path.

The Python save_screenshot() method writes the current browsing context to a PNG file. The captured area depends on the active browser window or headless viewport, so set the window size before visiting the page when layout breakpoints matter.

A screenshot can expose private page content, cookies rendered in the UI, account names, or internal URLs. Save images into a known artifacts directory, sanitize them before attaching to tickets, and wait for the page state that proves the issue before calling save_screenshot().

Steps to capture a screenshot with Selenium:

  1. Create a directory for screenshot artifacts.
    $ mkdir -p artifacts
  2. Create capture_screenshot.py with a headless Chrome session and screenshot output path.
    capture_screenshot.py
    from pathlib import Path
    import shutil
     
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
     
     
    output_dir = Path("artifacts")
    output_dir.mkdir(exist_ok=True)
    output_file = output_dir / "example-home.png"
     
    options = Options()
    options.add_argument("--headless=new")
    options.add_argument("--window-size=1280,720")
     
    for browser_name in ("google-chrome", "chromium", "chromium-browser"):
        browser_path = shutil.which(browser_name)
        if browser_path:
            options.binary_location = browser_path
            break
     
    driver_path = shutil.which("chromedriver")
    service = Service(driver_path) if driver_path else None
     
    driver = webdriver.Chrome(service=service, options=options)
    try:
        driver.get("https://example.com/")
        saved = driver.save_screenshot(str(output_file))
        print(f"title: {driver.title}")
        print(f"screenshot_saved: {saved}")
        print(f"path: {output_file}")
    finally:
        driver.quit()

    If Chrome and ChromeDriver are already resolved by Selenium Manager, the explicit path lookup is harmless. If launch fails before navigation, configure the driver first.
    Related: How to configure ChromeDriver for Selenium

  3. Run the script to open the page and write the PNG.
    $ python3 capture_screenshot.py
    title: Example Domain
    screenshot_saved: True
    path: artifacts/example-home.png

    For dynamic pages, wait for the visible element or state that proves the page is ready before saving the image.
    Related: How to use explicit waits in Selenium
    Related: How to wait for page load in Selenium

  4. Confirm that the screenshot file was created.
    $ ls -lh artifacts/example-home.png
    -rw-r--r-- 1 user user 18K Jun 15 21:09 artifacts/example-home.png

    Review screenshots before sharing them. Browser evidence can contain account names, private records, session-specific UI state, or internal hostnames.