DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Take Screenshots with Selenium in Python, Java, and C#

Learn Selenium screenshot methods for Python, Java, and C#, including page and element captures, iframe context, and common fixes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Selenium’s driver-level screenshot method to capture the current browsing context, or the element-level method to capture one element. Python saves directly with save_screenshot(); Java uses TakesScreenshot; C# uses ITakesScreenshot. The examples below show both approaches, explain frame selection, and save PNG files.

Choose a page or element screenshot

A driver screenshot captures the current browsing context. Use it for the page view Selenium is currently controlling. An element screenshot targets one located element, so you do not have to capture a page and crop it yourself.

Do not assume a normal driver screenshot is always a full-page image. Screenshot extent can depend on the browser and driver; the cited Selenium examples do not establish a universal full-page rule. See Selenium’s window and tab documentation for the language examples.

Python

Save the current browser screenshot

Install Selenium and have Chrome available with a compatible driver setup, then run:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium import webdriver

 driver = webdriver.Chrome()
try:
    driver.get("https://www.example.com")
    driver.save_screenshot("./image.png")
finally:
    driver.quit()

The driver method writes the screenshot to the supplied path. To save only one element, locate it and call its screenshot method:

from selenium import webdriver
from selenium.webdriver.common.by import By

 driver = webdriver.Chrome()
try:
    driver.get("https://www.example.com")
    element = driver.find_element(By.CSS_SELECTOR, "h1")
    element.screenshot("./element.png")
finally:
    driver.quit()

If you need image data rather than a file, Python also provides get_screenshot_as_png(), which returns PNG bytes. The Python API documents this alongside its screenshot methods: WebDriver Python API.

Java

Save the current browser screenshot

This example uses Selenium’s Java binding and Apache Commons IO to copy the returned temporary file to the desired path. Add Commons IO as a project dependency if using FileUtils.

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScreenshotExample {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://www.example.com");
            File screenshotFile = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotFile, new File("./image.png"));
        } finally {
            driver.quit();
        }
    }
}

Capture one element

Locate a WebElement and request its screenshot file instead of calling the driver method:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
WebElement element = driver.findElement(By.cssSelector("h1"));
File elementFile = element.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(elementFile, new File("./element.png"));

These Java calls follow Selenium’s documented screenshot examples at Working with windows and tabs.

C#

Save the current browser screenshot

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

var driver = new ChromeDriver();
try
{
    driver.Navigate().GoToUrl("https://www.example.com");
    Screenshot screenshot = (driver as ITakesScreenshot).GetScreenshot();
    screenshot.SaveAsFile("screenshot.png", ScreenshotImageFormat.Png);
}
finally
{
    driver.Quit();
}

Capture one element

Use the element’s screenshot interface for a targeted capture:

IWebElement element = driver.FindElement(By.CssSelector("h1"));
Screenshot screenshot = (element as ITakesScreenshot).GetScreenshot();
screenshot.SaveAsFile("element.png", ScreenshotImageFormat.Png);

The documented ScreenshotImageFormat values include BMP, GIF, JPEG, PNG, and TIFF. The example explicitly saves PNG. See Selenium’s language examples.

Capture content inside an iframe

WebDriver commands operate in the currently selected browsing context. To locate or capture iframe content, switch into the frame first. Selenium documents selecting a frame by its element, name or ID, or index; use the default-content switch to return to the top-level page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
// Java example: switch into a frame element, then back out
WebElement frame = driver.findElement(By.cssSelector("iframe"));
driver.switchTo().frame(frame);
WebElement framedElement = driver.findElement(By.cssSelector("h1"));
File image = framedElement.getScreenshotAs(OutputType.FILE);
driver.switchTo().defaultContent();

Adapt the frame-switching syntax to your binding. The key is to select the frame before issuing commands for its contents. See Selenium’s frame documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What Selenium returns

Selenium describes the WebDriver screenshot endpoint as returning screenshot data encoded in Base64. Language methods make that data easier to use: Python can write a file or return PNG bytes, Java can request a file with OutputType.FILE, and C# returns a Screenshot that can be saved with a chosen image format. The exact output and capture extent remain tied to the binding method and the browser driver.

Troubleshooting

  • The screenshot is of the wrong page or tab: select the intended window or tab before capture. A driver screenshot applies to the current browsing context.
  • An element inside an iframe cannot be found: switch into that frame before locating it; switch back to default content when finished.
  • The output file is missing: check that the process can write to the chosen path and that its parent directory exists. Use an explicit path if the process working directory is not what you expect.
  • The Java example does not compile at FileUtils: Apache Commons IO is an additional dependency in that sample; add it or copy the screenshot file using another file-copy method.
  • The image does not include the whole page: do not infer full-page support from a driver screenshot call alone. Check the behavior documented for your browser and driver, or capture a specific element when that is what you need.
  • The browser remains open after an error: put quit() in a finally block as shown, so the session is closed even if navigation or saving fails.

Or skip the browser setup

ScreenshotNeo can return a screenshot or PDF through one GET request, without setting up a browser driver. Its cleanup accepts cookie or consent banners as a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. It also provides an MCP server with take_screenshot, get_page_info, and capture_pdf tools for AI agents.

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

See the ScreenshotNeo documentation for the API details. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. ScreenshotNeo is made by Yorker Media. Sign up for free.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.