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’s Java API, getScreenshotAs(OutputType<X>) is declared by org.openqa.selenium.TakesScreenshot. A driver or element that supports screenshots can provide the implementation; RemoteWebDriver, for example, implements it. The method returns the same capture in the representation you request—Base64 text, a byte array, or a temporary file. That choice does not turn the standard driver screenshot into a full-page capture: the WebDriver screenshot is viewport-oriented.

Where the method is declared and implemented

TakesScreenshot is the Selenium interface that declares the method. Its generic signature is:

<X> X getScreenshotAs(OutputType<X> target)

The interface describes a capability, not a concrete browser implementation. A driver must support that capability for the call to work. Selenium lists browser drivers and remote drivers among its implementations; RemoteWebDriver supplies a public implementation. WebElement is also a known subinterface, so an element that supports screenshots can expose the same method.

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

In typical Java code, make the capability explicit by casting a driver to TakesScreenshot:

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

That cast does not add screenshot support to a driver that lacks it. If the underlying implementation does not support capture, Selenium documents UnsupportedOperationException as a possible result.

What happens when you call getScreenshotAs

At the WebDriver protocol level, the standard screenshot operation captures the top-level browsing context’s visual viewport. The driver screenshot endpoint is GET /session/{session id}/screenshot. The WebDriver specification describes the result as a lossless PNG snapshot returned to the local end as a Base64 string. Selenium then converts that protocol data into the Java type selected by OutputType.

Conceptually, the call has two separate parts: the browser driver captures an image, and Selenium represents the returned image as a Java value. Changing OutputType changes the second part, not the region captured by the first.

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

Choose the right OutputType

OutputType Java result When it is useful Important detail
OutputType.FILE File When another API or filesystem workflow expects a file. The file is temporary and Selenium says it is deleted when the JVM exits. Copy it to a permanent location if you need to keep it.
OutputType.BYTES byte[] When your code will write, inspect, or pass the image as binary data. The returned bytes represent the captured image; the capture scope is still determined by the driver operation.
OutputType.BASE64 String When a downstream consumer expects Base64-encoded image data. Decode the string only when a consumer needs the image bytes or a file.

Save a temporary file to a permanent path

This helper uses Java’s standard file APIs rather than a separate file-copy utility. It assumes you already created a Selenium WebDriver and navigated it to the page you want to capture:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public final class Screenshots {
    private Screenshots() {}

    public static Path saveViewport(WebDriver driver, Path destination)
            throws IOException {
        File temporaryImage = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        return Files.copy(temporaryImage.toPath(), destination,
                StandardCopyOption.REPLACE_EXISTING);
    }
}

Pass a destination such as Path.of("./image.png"). The extension is a filename choice; it does not select a different screenshot format. The standard WebDriver screenshot described in the specification is PNG.

Keep the image in memory

Use BYTES when you want to write the image yourself or pass it to code that consumes binary data:

byte[] image = ((TakesScreenshot) driver)
        .getScreenshotAs(OutputType.BYTES);
Files.write(Path.of("./image.png"), image);

Use BASE64 when the receiving system expects encoded text. If you need bytes locally, decode the result:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import java.util.Base64;

String encoded = ((TakesScreenshot) driver)
        .getScreenshotAs(OutputType.BASE64);
byte[] image = Base64.getDecoder().decode(encoded);
Files.write(Path.of("./image.png"), image);

These examples assume the usual Selenium imports and an initialized driver; they do not include browser-specific driver setup because that setup depends on the browser and environment you use.

Driver screenshots and element screenshots are different

Capture call Standard scope What not to assume
Driver: ((TakesScreenshot) driver).getScreenshotAs(...) The visual viewport of the top-level browsing context. It is not a promise of an image containing the whole scrollable page.
Element: element.getScreenshotAs(...) The visible region within the element’s bounding rectangle, after scrolling the element into view. It does not necessarily include content outside the visible element region.

The element operation uses a separate WebDriver endpoint, GET /session/{session id}/element/{element id}/screenshot. If you need a full-page image, do not assume that changing from FILE to BYTES or BASE64 will expand the capture. Selenium separately documents a Firefox full-page screenshot extension, getFullPageScreenshotAs; that is a distinct capability, not the default behavior of getScreenshotAs.

Driver conformance and differences between implementations

Selenium’s interface documentation says W3C-conformant drivers and elements follow the WebDriver specification. For nonconformant implementations, Selenium describes results as browser-dependent best effort. Depending on implementation, a driver capture may cover the whole page, the current window, a visible frame portion, or the entire display. An element capture may include all of the element’s content or only its visible portion.

For predictable automation, treat the standard viewport and visible-element definitions as the baseline, and verify any extension or nonconformant driver behavior in the specific browser and driver you deploy. The specification frames screenshots as visual diagnostic information; an image is not a substitute for checking the page state or the underlying test assertion.

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

Errors and troubleshooting

  • UnsupportedOperationException: the driver or element implementation does not support screenshot capture. Check the capability of the actual implementation in use; a cast to TakesScreenshot alone cannot provide it.
  • WebDriverException: Selenium documents this as a possible failure from the interface. Check that the driver session is still usable and that the browser operation has not failed before retrying.
  • The image shows only part of the page: this is expected for the standard driver screenshot, which is viewport-oriented. Use a supported full-page extension if available, or capture specific elements when that matches the need.
  • The saved file disappears: OutputType.FILE gives a temporary file. Copy it to the final destination while the JVM is running.
  • The element image is cropped: the standard element screenshot is limited to the element’s visible region after Selenium scrolls it into view. Do not interpret it as a guaranteed capture of all off-screen element content.
  • Different drivers return different regions: Selenium documents browser-dependent best-effort results for nonconformant implementations. Confirm the driver’s conformance and behavior rather than assuming every browser captures the same area.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

The API and specification establish what representation is returned and what the standard capture scope is; they do not establish a fixed capture time, a universal reliability rate, or a cost per screenshot. In practice, the selected representation affects how your application handles the returned data: a file is convenient for filesystem workflows, bytes avoid an additional Base64 decoding step when binary data is needed, and Base64 is useful when encoded text is required. Choose based on the consumer rather than expecting one option to make the browser capture faster or larger.

For automated test runs, save screenshots when they help diagnose failures and keep the captured scope in mind. A viewport screenshot can document what was visible without generating an unexpectedly long image, while an element screenshot can focus evidence on one component. Neither output type changes the underlying browser state or repairs a failed navigation.

Or skip the browser setup

If your goal is to capture a public page by URL rather than take an image from an existing Selenium session, ScreenshotNeo is a separate website screenshot API. It is not an implementation of Selenium’s Java method and does not capture the current state of your Selenium-controlled browser. One request can return an image or PDF; the API accepts a URL and offers PNG, JPEG, or WebP output as well as PDF.

Here is the one-call cURL form; see the ScreenshotNeo API documentation for request options:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Equivalent one-request examples are available in Python and Node.js:

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’s differences are specific: it accepts cookie or 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. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server offers take_screenshot, get_page_info, and capture_pdf tools for AI agents, including Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots. See ScreenshotNeo for the service details, or sign up free for 1,000 screenshots a month with no card.

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.