How to take a Selenium screenshot: capture a webpage in Python
Capture a Selenium browser window as PNG in Python, save or process the image, and understand element and Firefox full-page screenshots.
To save the currently visible Selenium browser window as a PNG in Python, call driver.save_screenshot("screenshot.png"). Check its Boolean return value: Selenium documents False when writing the file fails. This captures the current window; it does not mean the entire page document was captured.
1. Capture the current browser window
Install Selenium in the Python environment where you will run the script:
python -m pip install selenium
Then navigate with a WebDriver session, save the screenshot, and always close the browser:
from selenium import webdriver
url = "https://example.com"
output_path = "screenshot.png"
driver = webdriver.Chrome()
try:
driver.get(url)
saved = driver.save_screenshot(output_path)
if not saved:
raise OSError(f"Could not write screenshot to {output_path}")
finally:
driver.quit()
This is a usage pattern based on Selenium’s documented API; it is not a claim that the example was executed. Selenium’s Python bindings FAQ also points to save_screenshot for capturing the current window. [Selenium documentation]
Choose a writable path
A relative path such as screenshot.png is resolved from the process’s current working directory, which may differ from the script’s directory. Use an absolute path when the destination must be predictable, and make sure the process can write to its parent directory. Keep the .png extension: these Selenium file methods produce PNG output.
2. Choose file, bytes, Base64, or element capture
| Need | Method | Result |
|---|---|---|
| Write a PNG file | driver.save_screenshot(path) or driver.get_screenshot_as_file(path) |
Boolean indicating whether the file was written |
| Process or upload the image in memory | driver.get_screenshot_as_png() |
PNG bytes |
| Embed or pass encoded image data | driver.get_screenshot_as_base64() |
Base64-encoded screenshot |
| Capture a specific page element | element.screenshot(path) |
PNG screenshot of that element |
The driver screenshot methods capture the current window. The element method is useful when the output should contain a chart, card, or other single element rather than the whole visible browser area. These APIs and their return forms are documented in Selenium’s Python WebDriver API. [WebDriver API] [WebElement API]
Keep the screenshot in memory
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get("https://example.com")
png_bytes = driver.get_screenshot_as_png()
encoded = driver.get_screenshot_as_base64()
# Pass png_bytes to an image-processing or upload function.
# Pass encoded to a consumer that expects Base64.
Use bytes when the next step accepts binary data, such as an image-processing library or object-storage upload. Base64 is convenient for text-based transport but adds encoding overhead. Avoid writing to disk and reopening the file if your next step can consume the returned bytes directly.
Capture one element
from selenium import webdriver
from selenium.webdriver.common.by import By
with webdriver.Chrome() as driver:
driver.get("https://example.com")
card = driver.find_element(By.CSS_SELECTOR, "main")
saved = card.screenshot("main.png")
if not saved:
raise OSError("Could not write element screenshot")
The selector must match an element that exists on the page. If the element is absent, Selenium raises an element lookup error before screenshot capture. If it exists but is not ready for the intended image, wait for the page-specific condition before calling screenshot.
3. Current window versus full document
A standard driver.save_screenshot call captures the current window. It should not be described as a guaranteed full-document screenshot. If the requirement is to capture content beyond the visible window, identify the browser and driver first: the references here document full-page screenshot methods specifically on Firefox WebDriver.
from selenium import webdriver
with webdriver.Firefox() as driver:
driver.get("https://example.com")
saved = driver.get_full_page_screenshot_as_file("full-page.png")
if not saved:
raise OSError("Could not write full-page screenshot")
Firefox’s API also documents save_full_page_screenshot and get_full_page_screenshot_as_png. Check the current Firefox WebDriver API for availability in the version you use. Do not assume these Firefox methods are universal Selenium calls or behave identically in other browser drivers. [Firefox WebDriver API]
4. Decide when the page is ready
Navigation and screenshot capture are separate operations. A screenshot call does not establish that every image, font, animation, or application-specific component has reached the state you want. Choose a readiness condition based on the page: for example, wait until a particular element appears or until an application state is visible, then capture. The right condition depends on the site; the references for these screenshot methods do not define a universal page-readiness rule.
For pages with lazy-loaded content, a current-window screenshot shows the current window, and content outside it may not have been requested yet. A full-document screenshot method and a strategy for loading lazy content are separate concerns. Determine the required capture scope and page behavior explicitly instead of assuming that one screenshot call handles both.
5. Troubleshooting
| Symptom | Likely cause | What to check |
|---|---|---|
The method returns False |
The screenshot could not be written, such as from an invalid or unwritable destination. | Use a valid filename with a .png extension, check the parent directory and permissions, and inspect the Boolean result. |
| The file is missing from the expected folder | A relative path is resolved from the process working directory. | Print or inspect the working directory, or pass an absolute path. |
| The screenshot is blank or incomplete | The page may not yet be in the desired state, or the chosen capture covers only the current window. | Wait for a page-specific readiness condition and confirm whether you need a window, element, or full-document capture. |
| Element screenshot fails before writing | The selector did not find the intended element, or the element is not available in the current page state. | Check the selector and wait for the target element before locating and capturing it. |
| Full-page method is unavailable | The documented methods in this guide are Firefox WebDriver methods. | Use the Firefox driver API for its supported method, or consult the documentation for your specific browser and Selenium version. |
| The browser stays open after an error | Cleanup was skipped on an exceptional path. | Use try/finally with driver.quit(), or a WebDriver context manager. |
6. Performance, reliability, and cost
Screenshot cost in a Selenium script includes starting and controlling a browser, loading the target page, and storing or transmitting the image. Reuse a WebDriver session when capturing several pages if that fits the job, and close it in a guaranteed cleanup path. For large batches, bound concurrency to the available browser and machine resources; each browser session consumes resources, and excessive parallel sessions can make runs unreliable.
For repeatable output, make the browser, viewport, page state, and capture scope explicit in your automation. A screenshot can vary when page content changes or when capture happens at a different readiness point. Keep failures visible: check file-method return values, handle navigation and element errors, and record which URL and output path were involved. Selenium itself does not establish the target site’s availability or guarantee that dynamic content has finished rendering.
7. Or skip the browser setup
If you need a screenshot without managing a Selenium browser, ScreenshotNeo is a website screenshot API and MCP server for developers. A GET request returns an image or PDF. Its clean-shot flow accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Only clean shots are billed: bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses report the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.
For this Python workflow, the equivalent direct call is:
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()
with open("shot.webp", "wb") as image_file:
image_file.write(r.content)
See the ScreenshotNeo API documentation for request options. The API also accepts the parameter names used by other screenshot APIs. ScreenshotNeo offers full-page capture with lazy images loaded, element capture by CSS selector, device and viewport settings, custom CSS and JavaScript, waits, cookies and headers, image formats, PDF output, caching, async jobs, bulk capture, and more. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots, and every feature is available on every plan. Yearly billing gives two months free. Visit ScreenshotNeo for product details.
Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed; an MCP server lets AI agents take screenshots; and 1,000 screenshots a month are free with no card, with paid plans starting at $5 for 3,000. Sign up for the free plan to get started.
8. FAQ
Does Selenium save screenshots as JPEG?
The file methods described here save PNG screenshots. Convert the PNG with an image library if your downstream workflow requires another format.
Can I return the screenshot from a script instead of saving it?
Yes. Use get_screenshot_as_png() for bytes or get_screenshot_as_base64() for encoded data, then pass the result to the next step in your application.
Which method should I use for a page section?
Locate the element and call its screenshot(path) method. Use a driver-level screenshot when you want the current browser window.
Does save_screenshot capture the whole page?
It captures the current window. The full-document methods identified here are Firefox WebDriver APIs; check the documentation for the browser you actually run.


