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

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 3.6 for Java, cast your WebDriver to TakesScreenshot and call getScreenshotAs. To keep the image after the program exits, copy the temporary file returned by OutputType.FILE to a destination you choose. The example below captures the current browser view, saves it as screenshot.png, and closes the driver even if capture or copying fails.

Capture and save a screenshot

This example uses the Selenium 3.6.0 Java API and Java’s built-in file-copy support, so it does not require Apache Commons IO. It assumes a ChromeDriver is already available and configured for your environment; the screenshot code itself is the same pattern for any driver implementation that supports screenshots.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CaptureScreenshot {
    public static void main(String[] args) throws IOException {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com");

            File temporaryScreenshot =
                ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            Path destination = Paths.get("screenshot.png");
            Files.copy(temporaryScreenshot.toPath(), destination,
                       StandardCopyOption.REPLACE_EXISTING);

            System.out.println("Screenshot saved to: " +
                               destination.toAbsolutePath());
        } finally {
            driver.quit();
        }
    }
}

Compile against Selenium’s Java library, using Selenium 3.6.0 if you specifically need that release. The 3.6.0 package includes both TakesScreenshot and OutputType. You also need a browser driver that can be started in your environment. This example does not configure or download ChromeDriver: driver installation and browser compatibility are separate setup concerns and depend on the browser and driver versions you use.

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.

What each step does

  1. driver.get(...) navigates to the target page. If the page depends on asynchronous rendering, wait for the relevant content before taking the shot; otherwise, the capture may reflect an earlier state.
  2. ((TakesScreenshot) driver) uses the screenshot interface implemented by a supporting driver. The cast does not add screenshot capability to a driver that lacks it.
  3. getScreenshotAs(OutputType.FILE) returns a temporary image file.
  4. Files.copy(...) copies that temporary file to a named destination. REPLACE_EXISTING means a prior file with that name is overwritten.
  5. The finally block calls driver.quit() whether the navigation, screenshot, or file copy succeeds or throws an exception.

The example uses a relative destination, so screenshot.png is written relative to the process’s working directory. Use an explicit path if a test runner or scheduled job may start in a different directory. The process must have permission to create or replace the file there.

Choose the screenshot output type

OutputType controls how Selenium returns the screenshot data. It changes the representation, not the page area being captured.

Output type What you receive When it fits Important detail
OutputType.FILE A temporary File Saving a test artifact to disk Copy it to a destination you control before the JVM exits; the temporary file is documented for deletion at JVM shutdown.
OutputType.BYTES Raw screenshot bytes In-memory processing or passing image data to code that accepts bytes No intermediate temporary file is needed, but you must decide how and where to persist the bytes if you need a durable artifact.
OutputType.BASE64 A Base64-encoded string A following step expects encoded image data It is encoded data, not a file path; decode it if the next step needs a binary image.

For example, to keep the capture in memory, replace OutputType.FILE with OutputType.BYTES and store the returned byte array. For encoded data, use OutputType.BASE64 and keep the returned string. The Java API describes screenshots as being storable in different ways; choose the form that fits the next step in your test pipeline.

Know what area the call captures

The basic driver call is for the current browsing context. Do not assume that it guarantees a full-page image across every browser, driver, and Selenium version. Screenshot extent can depend on the implementation and its protocol conformance; Selenium’s general API documentation describes best-effort behavior for drivers that do not conform to the W3C WebDriver standard. That is useful context, but it is not a browser-by-browser compatibility matrix for Selenium 3.6.

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

Likewise, although the screenshot interface description refers to a driver or HTML element, that is not enough to guarantee element-specific capture support for every Selenium 3.6 driver. Check the specific driver behavior you need rather than inferring full-page or element screenshots from the output type. If the requirement is a reliable full-page capture or a single-element image, validate it with the exact browser and driver combination that will run your tests.

Make the saved artifact useful in a test run

Use a predictable path

A filename such as screenshot.png is sufficient for a one-off example, but repeated test runs can overwrite it. In a test suite, choose a path that is unique per test or run, and place it in a directory your test runner preserves as an artifact. Create parent directories before copying if they do not already exist. These are Java file-handling considerations; Selenium does not choose your test artifact directory.

Capture the state you intend to diagnose

Take the screenshot after the browser reaches the state you want to inspect. A page can load its initial document before client-side content, images, or other visible elements have finished appearing. Waiting for the application condition you care about is more meaningful than adding an arbitrary pause, where your test framework and page allow it. The screenshot API captures what the browser presents at the time of the call; it does not itself verify that the page is ready or correct.

Keep cleanup reliable

Place driver.quit() in a finally block, as shown, so an exception during capture or saving does not skip browser cleanup. If cleanup itself needs special handling in your application, keep that separate from the screenshot copy operation so a failed artifact write does not leave the browser process running.

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

Troubleshoot common failures

Symptom Likely cause What to check
A cast or screenshot call fails The concrete driver does not support screenshot capture, or the implementation reports the operation as unsupported. Confirm the driver implements screenshot capture and supports it for your browser and version. Selenium can report unsupported operation or a WebDriver failure.
The destination file is missing after the program exits The returned OutputType.FILE file was treated as permanent, or the copy did not complete. Copy the temporary file before process shutdown, check for exceptions, and print or inspect the destination’s absolute path.
The copy throws an I/O exception The destination directory may not exist, may not be writable, or the file operation otherwise failed. Use a known writable location, create missing parent directories, and check the exception message and path.
A later run has replaced the prior image The same destination name is used each time and the sample explicitly replaces existing files. Use a unique filename or remove REPLACE_EXISTING if replacement should instead fail when the path already exists.
The image is blank, incomplete, or not full-page The capture happened before expected content appeared, or the browser/driver’s capture extent differs from what the test assumes. Wait for a page-specific condition and verify the exact browser and driver behavior. The basic call does not promise full-page capture universally.
The returned screenshot is not in the form the next step expects FILE, BYTES, and BASE64 were treated as interchangeable. Choose the output representation needed by the consumer, and explicitly copy, persist, or decode it as appropriate.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot without configuring Selenium and a local browser driver, ScreenshotNeo provides a website screenshot API and an MCP server for developers. A single request can return a PNG, JPEG, WebP, or PDF. Here is the cURL form, using the API’s documented endpoint and parameter names:

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

See the ScreenshotNeo API documentation for request options and response details. Before capture, it can accept cookie or consent banners and remove more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents using Claude, Cursor, or another MCP client.

The free plan includes 1,000 shots per month with no card required. Paid plans start at $5 for 3,000 shots; all listed features are available on every plan. If that fits your use case, sign up for ScreenshotNeo free and get 1,000 screenshots a month without a card.

Frequently Asked Questions

Does the Selenium screenshot call prove that the page passed the test?

No. It captures an image of the browser state; your test still needs assertions that check the expected page or application behavior.

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.