Skip to content

How to take a screenshot with Selenium

Selenium WebDriver can write a screenshot to a file with a single method call.

OUTPUT FORMATSHTMLJSONMarkdown

The captured workflow is short: start a driver, navigate to the URL, then pass an output path to save_screenshot. This guide walks through that sequence and the checks that help prevent the most common setup problems.

The core method: save_screenshot

The captured example takes a screenshot through the WebDriver's save_screenshot method. In Python, the call takes the form driver.save_screenshot(screenshot_path), where screenshot_path is the destination file. The captured example uses a .png path. The source does not document a return value, so treat the call as a write step and verify the file on disk afterward if your pipeline needs confirmation.

Minimum working sequence

The sequence below reflects the captured example. It imports webdriver, sets a DRIVER_PATH constant pointing at the chromedriver executable, instantiates Chrome with executable_path, navigates to a URL, and saves the screenshot to a path variable.

Pre-run checklist

Before running the script, confirm four things. First, the chromedriver executable exists at the path assigned to DRIVER_PATH. Second, the target URL is reachable from the machine running the driver. Third, the directory containing screenshot_path exists and is writable by the process. Fourth, decide whether to close the driver after the screenshot; the captured example does not include a quit step, so add one if you want the browser process to end cleanly. Each item maps to a distinct failure mode, described next.

Failure distinctions

Two failures look similar but have different causes. A driver-start failure occurs before any page is loaded: the executable_path value does not resolve to a runnable chromedriver, so the browser never starts. A navigation failure occurs after the driver starts: the URL is unreachable or the page errors, so there is nothing meaningful to capture. A third, separate failure is a write failure: the driver and page are fine, but screenshot_path points to a missing directory or a location the process cannot write. Check the driver path first, then the URL, then the output directory.

What the captured example does and does not cover

The captured example navigates to a URL and saves a screenshot to a file path. It does not resize the browser window, scroll the page, or capture a specific element, so those variations are outside what the source demonstrates. If you need a different capture scope, treat it as an extension of the basic sequence rather than something the captured example already handles.

Related Selenium tasks

Screenshot capture is one step in a broader Selenium workflow. The related questions below cover element location, page source retrieval, scrolling, waiting for load, driver path issues, and cookie handling.

Frequently asked questions

What does the captured example actually do?

It starts a Chrome driver from a DRIVER_PATH, navigates to a URL with driver.get, and writes a screenshot to screenshot_path with driver.save_screenshot.

Does the captured example close the browser afterward?

No. The captured example ends after save_screenshot. If you want the browser process to end cleanly, add a quit step after the screenshot.

Why does the script fail before the browser opens?

The most common cause is that the chromedriver executable at DRIVER_PATH is missing or not runnable. Verify the path before rerunning.