How to Screenshot a Website with Selenium in Python
Use Selenium in Python to open a website and save a PNG screenshot. Learn how to capture an element, handle output, and troubleshoot common issues.
To screenshot a website with Selenium in Python, open a WebDriver, navigate to the page, and call driver.save_screenshot("screenshot.png"). The method saves a PNG of the current browser window. Close the driver when you are done.
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get("https://example.com")
saved = driver.save_screenshot("screenshot.png")
if not saved:
raise OSError("Could not save screenshot.png")
This captures the current window, not a guaranteed full-page image. Selenium’s API documents a PNG screenshot of the current window and a boolean return value: false indicates an I/O error. Use a clear output path and a .png filename. See Selenium’s window interaction guide and Python WebDriver API.
1. Install Selenium and prepare a browser
Install the Python package in your environment:
python -m pip install selenium
Install or make available a compatible browser such as Chrome. Selenium’s webdriver.Chrome() starts a Chrome session; the exact browser and driver setup can depend on your operating system, browser version, and Selenium version. If startup fails, see the troubleshooting section below.
2. Save a screenshot of the current window
Use an explicit output path so you know where the file will be written:
from pathlib import Path
from selenium import webdriver
output = Path("/tmp/example-window.png")
with webdriver.Chrome() as driver:
driver.get("https://example.com")
if not driver.save_screenshot(str(output)):
raise OSError(f"Selenium could not write {output}")
print(f"Saved {output}")
Replace /tmp/example-window.png with a location writable by the process. On Windows, use a raw string such as r"C:\\captures\\example.png", or construct the path with pathlib.Path. The context manager closes the driver session when the block exits, including when an exception is raised.
Control the browser window size
The screenshot is of the current window, so set a window size before navigating if a particular viewport is needed:
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.set_window_size(1440, 1000)
driver.get("https://example.com")
driver.save_screenshot("example.png")
This controls the browser window dimensions. It does not turn the method into a documented full-page capture. Selenium’s cited API material does not establish full-page behavior, automatic lazy-image loading, or pixel-identical results across browsers.
3. Capture one element instead of the window
For a single element, locate it and call the element’s screenshot() method. This example uses a CSS selector:
from selenium import webdriver
from selenium.webdriver.common.by import By
with webdriver.Chrome() as driver:
driver.get("https://example.com")
heading = driver.find_element(By.CSS_SELECTOR, "h1")
if not heading.screenshot("heading.png"):
raise OSError("Could not save heading.png")
The window method needs no locator and captures the current browser window. The element method requires a matching element and saves that element’s screenshot. Selenium’s official example demonstrates locating an h1 with CSS and calling WebElement.screenshot(); see the official example.
4. Get screenshot data instead of writing a file
If another part of your program will upload or process the screenshot, Selenium can return PNG bytes or a base64 string:
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get("https://example.com")
png_bytes = driver.get_screenshot_as_png()
base64_png = driver.get_screenshot_as_base64()
with open("example.png", "wb") as image_file:
image_file.write(png_bytes)
Use get_screenshot_as_png() for binary image data, such as an upload body or file write. Use get_screenshot_as_base64() when the next interface specifically expects base64 text. These are alternative representations of a screenshot; they do not change its capture scope from the current window. The methods are documented in Selenium’s Python WebDriver API.
5. Wait for the page condition you need
driver.get() navigates to a URL, but a page may continue changing after navigation, for example because JavaScript updates its content. If you need a particular element to exist before capture, wait for it explicitly:
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
with webdriver.Chrome() as driver:
driver.get("https://example.com")
WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "h1"))
)
driver.save_screenshot("example.png")
The wait example uses Selenium’s explicit-wait API. Choose a condition that matches what the screenshot must show: presence means the element is in the DOM, while visibility is a stronger condition when it must be displayed. A fixed sleep can be simpler, but it may waste time on fast pages or still be too short on slow ones.
6. Or skip the browser setup
If you only need a screenshot file from a URL, ScreenshotNeo offers a website screenshot API and MCP server. One GET request returns an image or PDF; see the API documentation for options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for 1,000 free screenshots a month, no card required.
7. Troubleshooting
| Problem | Likely cause | What to do |
|---|---|---|
webdriver.Chrome() fails to start |
Chrome is missing, or the browser and driver setup is incompatible or unavailable. | Install Chrome and check the Selenium and browser setup for your environment. Read the full exception; startup failures occur before screenshot saving. |
| No screenshot appears | The path is not where you expected, the parent directory does not exist, or the process cannot write there. | Use an explicit absolute path, create the parent directory first, and check the boolean result from save_screenshot(). |
| The image is not the full page | save_screenshot() is documented as capturing the current window. |
Do not assume full-page behavior from this method. If full-page output is a requirement, choose a browser-specific approach only after checking its documentation for your browser and version. |
| The screenshot shows an unfinished page | The relevant content may load or render after navigation. | Wait for a meaningful element or other page condition before capturing. Confirm that the wait condition describes the content you need. |
| Element screenshot lookup fails | The selector matched no element at lookup time. | Check the selector and wait for the element before calling find_element(). |
| Screenshot output is empty or cannot be used downstream | The consumer may expect bytes, a saved path, or base64 text. | Use save_screenshot() for a file, get_screenshot_as_png() for bytes, or get_screenshot_as_base64() for base64. |
8. Performance, reliability, and cost
- Performance: Starting a browser session has setup overhead; reuse one session for multiple pages when that fits your workflow, and close it reliably. Wait for the condition you need rather than applying a long fixed delay to every page.
- Reliability: Always close the session with a context manager or
quit(). Use an explicit path, check the save result when file errors matter, and handle navigation and wait exceptions in the calling application. - Output size: PNG is the documented output for these Selenium screenshot APIs. The captured window dimensions influence the resulting image dimensions and file size.
- Cost: Selenium itself is open-source software, but browser execution still uses your compute resources. Your hosting or CI costs depend on where and how often you run it; no general per-screenshot cost can be inferred from the Selenium API.
9. Frequently asked questions
Does Selenium save screenshots as JPEG?
The documented save_screenshot() method saves a PNG. The API also provides PNG bytes and base64 PNG data; this guide does not claim a JPEG output option for these methods.
Can I screenshot an element without saving the whole window?
Yes. Locate a WebElement and call its screenshot("element.png") method.
What does save_screenshot() return?
It returns True when the screenshot is saved and False if an I/O error occurs, according to Selenium’s Python API.
Where is the file saved if I use a relative path?
It is written relative to the Python process’s current working directory. Use an absolute path when the output location must be unambiguous.


