ScreenshotNeo

BlogHow-to

How to Take Website Screenshots with Selenium and ChromeDriver

Capture a website or page element with Selenium and ChromeDriver in Python, save the PNG safely, and handle headless runs and common errors.

By the ScreenshotNeo team4 October 20267 min read

Selenium can take a PNG screenshot of the current browser window with driver.save_screenshot("screenshot.png"). Start Chrome with Selenium, open the page, save the screenshot, check the method’s Boolean result if you need to detect file-write failures, and close the browser with driver.quit(). The same workflow can run with Chrome in headless mode.

This guide is in English despite the title’s Telugu-language search phrasing. The examples below use Python, Selenium, and ChromeDriver.

1. Take a website screenshot with Selenium and ChromeDriver

Install Selenium in the Python environment you will use to run the script:

python -m pip install selenium

Then save this as screenshot.py and run it with python screenshot.py:

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 save screenshot to {output_path}")
finally:
    driver.quit()

Selenium’s driver-level save_screenshot(filename) saves the current window as a PNG. The API returns True when it succeeds and False when it encounters an IOError; its reference recommends providing a full path. The example uses a relative path, so the file is written relative to the script’s current working directory. For a predictable destination, replace output_path with a full path that exists and is writable.

The browser must be able to load the target page before the capture. driver.get() waits for the page’s load event under Selenium’s normal page-load strategy, but that does not guarantee that every later-running script, animation, or asynchronously loaded component has finished. If the page needs additional time, use an explicit wait for a known page condition before saving; avoid assuming that an arbitrary delay fits every site.

2. Run Chrome without a visible window

For a headless run, pass Chrome’s --headless argument when creating the driver. The capture call and cleanup stay the same:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")

url = "https://example.com"
output_path = "screenshot.png"

driver = webdriver.Chrome(options=options)
try:
    driver.get(url)
    if not driver.save_screenshot(output_path):
        raise OSError(f"Could not save screenshot to {output_path}")
finally:
    driver.quit()

Headless mode means Chrome runs without a visible browser window. It does not change the output format: save_screenshot still writes PNG. Browser behavior and environmental requirements can vary, so if a headless capture differs from a visible one, first compare the page, browser configuration, viewport, and timing.

3. Capture a specific page element

Use a WebElement’s screenshot method when you need one component, such as a chart or product card, rather than the current browser window. Locate the element, wait until it is available, then save it:

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

url = "https://example.com"
output_path = "element.png"

driver = webdriver.Chrome()
try:
    driver.get(url)
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "main"))
    )
    if not element.screenshot(output_path):
        raise OSError(f"Could not save element screenshot to {output_path}")
finally:
    driver.quit()

Change main to a CSS selector that identifies the element you want. A selector that matches nothing will time out in this example. Element screenshots are useful when the surrounding page is irrelevant; use the driver method when you want the current browser view.

4. Choose the capture scope

Method What it captures Use it when
driver.save_screenshot(path) The current browser window or browsing context as a PNG You need the browser view of the page.
element.screenshot(path) The located WebElement as an image You need one specific page component.

Do not treat the driver-level screenshot as a guaranteed full-page capture. The cited Python API describes saving the current window. If the requirement is a full-page image, verify that the chosen capture method provides the page extent you need rather than assuming the default window screenshot includes content outside the current view.

5. Make the capture more dependable

  1. Use a stable destination. Choose an absolute output path for scheduled jobs or services, and ensure its parent directory exists and is writable.
  2. Wait for page-specific readiness. When the screenshot depends on a particular element, wait for that element to become visible instead of relying only on the navigation completing.
  3. Check the save result. Handle the Boolean return value if the application needs to report an unsuccessful save.
  4. Always close the browser. Put driver.quit() in a finally block so it runs when navigation or file writing raises an exception.
  5. Keep each run isolated. In a larger script, create and close a driver for each controlled capture workflow, and avoid sharing one driver across unrelated concurrent tasks.

For a batch of URLs, reuse a browser session only when the pages can safely share its cookies and state. Otherwise, separate sessions help avoid one page’s state affecting another capture. In either case, make cleanup reliable and record the URL and output path associated with each failed capture.

6. Troubleshooting

Symptom Likely cause What to do
The script cannot start Chrome. Chrome or its driver cannot be started in the current environment, or the browser setup is incomplete. Check the Selenium and ChromeDriver setup guidance for your operating system and environment. The sources cited here do not establish a universal installation or browser-driver matching recipe.
The screenshot file is missing or the save method returns False. The destination is invalid, its parent directory does not exist, or the process cannot write there. Use a full path, create the parent directory beforehand, and check write permissions. Treat a False return as a failed save.
The page is captured before a chart or image appears. The content loads after the page navigation completes. Wait for a page-specific element or other condition that signals the content is ready, then capture.
The element screenshot times out. The CSS selector does not match, or the element never becomes visible within the wait period. Inspect the selector and confirm that the element is present and visible on the target page. Adjust the wait only when the page’s real load behavior justifies it.
The headless result differs from the visible result. The page or browser behaves differently under the current headless configuration, or the capture happens at a different readiness point. Compare a visible run, the loaded page state, and the browser options. Wait for the same page condition before each capture.
The browser process remains after an error. Cleanup was skipped on an exception path. Put the capture workflow inside try/finally and call driver.quit() in the finally block.

7. Performance, reliability, and cost

Most of the time in a screenshot workflow is spent starting the browser and loading the page, not writing the PNG. Reusing a driver can avoid repeated browser startup for a controlled batch, but a shared session also carries browser state between navigations. Choose between reuse and isolation based on whether that state is safe to share.

For reliability, wait on the content that matters, use a writable output location, check the save result, and close the driver even after failures. Selenium’s screenshot API writes a PNG; account for the storage required by the images your own workflow produces. Selenium itself does not remove consent banners, popups, or chat widgets as part of the documented screenshot calls described here, so those may appear in the captured page.

With a self-managed Selenium workflow, plan for the browser runtime and the infrastructure that runs it. If you do not want to manage browser setup and capture infrastructure, ScreenshotNeo offers a hosted screenshot API. Its billing rules and plans are described below.

8. Or skip the browser setup

ScreenshotNeo takes a website screenshot with one GET request. See the ScreenshotNeo API documentation for the available parameters and formats. This Python example saves the response body as an image:

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)

The equivalent cURL request is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

And in Node.js:

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 accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and each response reports the page verdict and billing status in headers. Its MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Learn about ScreenshotNeo, then sign up free for 1,000 screenshots a month with no card.

9. Frequently asked questions

What file format does Selenium save?

The documented Python save_screenshot method saves the current window as a PNG.

Can Selenium capture only one element?

Yes. Locate the WebElement and call its screenshot(path) method to save that element.

Does save_screenshot always capture the entire page?

The cited API describes the current window. Do not assume it captures content beyond the current browser view.

Can I use Selenium without showing Chrome?

Yes. Selenium’s documentation shows a Chrome options example with the --headless argument.

Sources