Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
In Selenium Python, call driver.save_screenshot("screenshot.png") to save the current browser window as a PNG. Use a path you can write to, and check the method’s Boolean return value if your code needs to detect a failed save. The example below shows the basic flow, followed by options for capturing an element or working with image data without writing directly to a file.
Save the current window as a PNG
The simplest approach is to open a page, save the screenshot, and then close the WebDriver session. This example uses Chrome and saves the file in the program’s current working directory:
from selenium import webdriver
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
saved = driver.save_screenshot("screenshot.png")
if not saved:
raise IOError("Selenium could not save screenshot.png")
finally:
driver.quit()
save_screenshot(filename) saves a PNG of the current window. Selenium’s Python API documents this method in version 4.49.0. It returns True when it saves successfully and False if an IOError occurs. The try/finally ensures the browser session is closed even if navigation or saving raises an exception.
Use a filename ending in .png. A relative filename such as screenshot.png is resolved from the process’s current working directory, which may not be the directory containing your script. To make the destination explicit, pass a full path, for example /tmp/screenshot.png on a Unix-like system or C:\Temp\screenshot.png on Windows. The directory must exist and be writable by the process.
#1 Best Overall
Save only after the page is ready
The call captures the browser at the moment it runs. If the page is still navigating or rendering, the image may not show the state you intended. For a simple page, placing the call after driver.get() may be enough; for pages that load content later, wait for a meaningful condition before saving. For example, wait for a particular element rather than relying on an arbitrary pause:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.TAG_NAME, "h1"))
)
if not driver.save_screenshot("screenshot.png"):
raise IOError("Screenshot was not saved")
finally:
driver.quit()
The condition and timeout should match the page and test: waiting for an element that never appears will time out, while a condition that appears too early may not mean the whole page is visually settled. Selenium’s screenshot method does not, by itself, promise to wait for every image, animation, or asynchronous update to finish.
Choose the output your code needs
Selenium’s Python API offers file, byte, and base64 forms of the current-window screenshot. Choose based on what consumes the result next:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →| Output | Python call | Use it when |
|---|---|---|
| PNG file | driver.save_screenshot("screenshot.png") |
You want Selenium to write the screenshot to a path. |
| PNG file alias | driver.get_screenshot_as_file("screenshot.png") |
You want the documented alias with the same file-saving description and return behavior. |
| PNG bytes | driver.get_screenshot_as_png() |
Your code will handle or transmit the image data directly. |
| Base64 text | driver.get_screenshot_as_base64() |
You need an encoded representation, such as for embedding an image in HTML. |
The bytes form can be written by your application instead of using Selenium’s file-saving method:
png_bytes = driver.get_screenshot_as_png()
with open("screenshot.png", "wb") as image_file:
image_file.write(png_bytes)
Base64 is text, not a PNG file’s raw bytes. If you write that text to a file, the result is not an ordinary PNG image unless you decode it first. Use the bytes method when you want PNG data to pass to another part of your program without encoding it as text.
Capture one element instead of the window
To save a particular web element, locate it and call the element’s screenshot method. Selenium’s guide shows this Python pattern:
Rank #3
from selenium.webdriver.common.by import By
element = driver.find_element(By.CSS_SELECTOR, "main")
element.screenshot("main.png")
Use a selector that identifies the element you actually need; replace main with the relevant CSS selector for the page. This is useful for an individual component, such as a chart or card, rather than a screenshot of the whole current window. The Python API’s ordinary driver screenshot is described as a current-window capture, so do not assume it produces a full-page image.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallWhat “current window” means—and what it does not
For the Python driver method, Selenium documents a screenshot of the current window. That is the safe expectation when using driver.save_screenshot(); it is not a guarantee of a full-page image. If your test needs content beyond the visible window, verify the behavior for the specific browser and driver setup rather than treating the ordinary method as a full-page capture.
Selenium’s Java TakesScreenshot API describes a separate compatibility caveat: for non-conformant driver or element implementations, screenshot behavior may be a browser-dependent best effort, with possible scopes ranging from an entire page to a current window, a visible frame portion, or an entire display. That fallback caveat is not a promise about Python captures or a universal description of conformant WebDriver implementations.
Selenium’s official WebDriver guide includes screenshot examples in Python, Java, C#, JavaScript, and Ruby. The API and return types differ across languages; the Python calls in this article should not be copied unchanged into another language’s binding.
Troubleshoot a missing or unexpected screenshot
The file is not where you expected
- Cause: A relative filename is being resolved from a different working directory than you assumed.
- Fix: Pass an explicit full path and check the process’s working directory if you intentionally use a relative path.
The save reports failure
- Cause: Selenium’s API documents
Falsefor anIOError; an invalid or unwritable destination is a common thing to check. - Fix: Check the Boolean result, ensure the parent directory exists, and confirm the process can write there. If your program needs to distinguish the failure from a successful capture, do not ignore the return value.
The screenshot shows an incomplete page
- Cause: The capture ran before the desired page state was visible, or the content extends beyond the current window.
- Fix: Wait for a page-specific element or condition before capturing. Treat full-page capture as a separate requirement; the standard Python method description only establishes a current-window screenshot.
The image is empty or the browser session has already closed
- Cause: The capture ran after the WebDriver session ended, or before the browser reached the page state the test expected.
- Fix: Keep the screenshot call inside the active session, place it after navigation and any required waits, and call
quit()only after the capture attempt.
Or skip the browser setup
If you need a website screenshot without launching and managing Selenium locally, ScreenshotNeo provides a screenshot API and an MCP server for developers. A single GET request can return a PNG, JPEG, WebP, or PDF. Here is a cURL example that saves a WebP response:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchescurl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options and response details. ScreenshotNeo accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers identify the page verdict and whether the request was billed. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients. The Free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots.
Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.
Frequently Asked Questions
Does Selenium save screenshots as JPEG by default?
The Python driver screenshot methods described here save a PNG. ScreenshotNeo is an option when you need a JPEG or WebP response instead.
Can I capture an element and the browser window in the same test?
Yes. Save the current-window image with the driver method and call the element’s screenshot method separately when you need an additional element image.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

