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

To get a Selenium element screenshot as a Java byte array, call element.getScreenshotAs(OutputType.BYTES). To turn those bytes into a BufferedImage, pass them to ImageIO.read through a ByteArrayInputStream. If you need a file instead, request OutputType.FILE and copy Selenium’s temporary file to a permanent path before the JVM exits.

These are screenshots of rendered pixels, not HTML or a serialized WebElement. The right output type depends on whether you need bytes in memory, an image object to process, a file, or Base64 text.

Get a WebElement screenshot as a byte array

Selenium’s TakesScreenshot API includes WebElement among its known subinterfaces. Its getScreenshotAs method accepts an OutputType; use BYTES when your next step needs image data in a Java byte[].

import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;

WebElement element = driver.findElement(By.cssSelector(".product-card"));
byte[] screenshotBytes = element.getScreenshotAs(OutputType.BYTES);

This requires a WebDriver instance and a located element, as well as a browser and driver implementation that supports element screenshots. The snippet assumes driver is already initialized and the page is ready for capture. Add the usual Selenium imports for your project, including By if you use the shown locator.

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

The returned bytes are encoded screenshot data, not raw pixel values or a Java image object. You can store the byte array, pass it to an image decoder, or send it to code that accepts encoded image bytes. If you need to manipulate pixels or inspect dimensions using Java’s image APIs, decode it to a BufferedImage.

Decode the bytes to a BufferedImage

Wrap the byte array in a ByteArrayInputStream, then call ImageIO.read. Check for a null result: Java returns null when none of its registered image readers recognizes the input. Close the stream when finished.

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.OutputType;

byte[] screenshotBytes = element.getScreenshotAs(OutputType.BYTES);
BufferedImage image;
try (ByteArrayInputStream input = new ByteArrayInputStream(screenshotBytes)) {
    image = ImageIO.read(input);
}
if (image == null) {
    throw new IOException("Screenshot bytes could not be decoded by an installed ImageIO reader");
}

The stream contains the encoded screenshot; ImageIO.read chooses a registered reader to decode it. The resulting BufferedImage can then be used with Java APIs or passed to other image-processing code. The method’s result can be null, so do not use the returned image without checking it.

The screenshot output is image data, not a representation of the element’s DOM, attributes, styles, or Selenium object state. If you need those, use Selenium’s element and browser APIs separately rather than trying to infer them from the screenshot.

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

Write a BufferedImage as a PNG

After decoding, use ImageIO.write to write the image in a chosen format. The method returns false if no suitable writer is available, so handle that result rather than assuming a file was produced.

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

boolean written = ImageIO.write(image, "png", new File("element.png"));
if (!written) {
    throw new IOException("No ImageIO writer was found for PNG");
}

PNG is among Java’s standard image writer formats. The output format here is selected by the string passed to ImageIO.write; it is separate from how Selenium initially returned the screenshot. Use a file path appropriate to your application, and ensure the process can write to its parent directory.

Save Selenium’s file output durably

If you prefer file-oriented capture, request OutputType.FILE and copy the returned temporary file to the destination you want to keep. Selenium documents this output as temporary and says it is deleted when the JVM exits. Do not treat the returned temporary path as durable storage.

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;

File temporaryScreenshot = element.getScreenshotAs(OutputType.FILE);
Files.copy(temporaryScreenshot.toPath(), Path.of("element.png"),
           StandardCopyOption.REPLACE_EXISTING);

REPLACE_EXISTING makes this example overwrite an existing destination file. Remove that option if overwriting is undesirable. The example uses Path.of; use an equivalent path construction method if your project’s Java version does not provide it.

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.

Choose the Selenium output that fits the next step

Need Output choice Result and next step
Image data in memory OutputType.BYTES A byte[] containing encoded screenshot bytes.
Java image object for processing OutputType.BYTES, then ImageIO.read A BufferedImage if an installed reader recognizes the image; check for null.
File-oriented capture OutputType.FILE A temporary file; copy it to durable storage before the JVM exits.
Text transport or embedding OutputType.BASE64 Base64-encoded screenshot data.

Choose the representation that matches the consumer. A byte array avoids a file step when another Java component accepts bytes. A BufferedImage is useful when image APIs need a decoded image. A file can be convenient for file-based processing, but Selenium’s returned file is temporary. Base64 is useful when the transport or embedding format is text, but it is still an encoded representation rather than a decoded Java image.

What element screenshots capture—and what can vary

An element screenshot records rendered pixels. For a W3C-conformant WebDriver or WebElement implementation, Selenium says the behavior follows the W3C WebDriver specification. With non-conformant implementations, capture is best effort and browser-dependent: an implementation may capture the full element content or only its visible portion.

That distinction matters for elements that extend beyond the viewport, are clipped, or have content that has not yet rendered. If the captured area does not match what you expect, first verify the browser’s rendered state and whether your driver’s element screenshot implementation follows the standard. Do not assume every browser and driver combination will produce identical element boundaries or off-viewport behavior.

Capture can also fail: Selenium documents a WebDriverException for screenshot errors and an UnsupportedOperationException when the underlying implementation does not support screenshot capture. Catching these exceptions may be appropriate in a larger workflow, but do not silently treat failure as a valid empty image.

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

Complete capture-and-decode example

This example brings the byte-array and image-decoding steps together. It assumes driver is an initialized Selenium WebDriver and that the target element exists and is ready to capture. It saves the decoded result as a PNG.

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;

public class ElementScreenshot {
    public static void saveElementPng(WebDriver driver) throws IOException {
        WebElement element = driver.findElement(By.cssSelector(".product-card"));
        byte[] screenshotBytes = element.getScreenshotAs(OutputType.BYTES);

        BufferedImage image;
        try (ByteArrayInputStream input = new ByteArrayInputStream(screenshotBytes)) {
            image = ImageIO.read(input);
        }
        if (image == null) {
            throw new IOException("Screenshot bytes could not be decoded by an installed ImageIO reader");
        }

        boolean written = ImageIO.write(image, "png", new File("element.png"));
        if (!written) {
            throw new IOException("No ImageIO writer was found for PNG");
        }
    }
}

The method declares IOException for image decoding or writing failures; Selenium capture and element lookup can raise Selenium exceptions as well. Add the appropriate driver setup, navigation, and wait logic for your page. If you only need the screenshot bytes, omit the decoding and file-writing portion.

Or skip the browser setup

If your goal is to capture a page from its URL rather than capture a particular element from an existing Selenium session, ScreenshotNeo offers a screenshot API. It can capture an element by CSS selector, but an API capture is not a way to reuse the exact browser state of an already running WebDriver session. Its response is a screenshot, not a Selenium WebElement.

For a page-level capture, this cURL request saves the returned image response. See the ScreenshotNeo API documentation for request options and response details.

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
  • Cookie banners, popups, and chat widgets are removed before capture; each cleanup step can be turned off.
  • Bot checks, blank pages, and failed loads are never billed. The response includes page-verdict and billing headers.
  • An MCP server provides screenshot tools for AI agents and MCP clients.
  • The Free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

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

Troubleshooting common problems

The screenshot call throws an exception

Check whether the selected browser and driver implementation supports element screenshot capture. Selenium documents UnsupportedOperationException when the implementation does not support the operation, and WebDriverException for capture errors. Confirm the element was located successfully and that the browser session is still active. If the implementation does not support element screenshots, use a supported capture path for that environment rather than assuming the call will work everywhere.

The image is null after ImageIO.read

ImageIO.read returns null when no registered reader can decode the stream. Check that the byte array is the screenshot result and has not been replaced, truncated, or otherwise altered between capture and decode. Keep the null check: a null image is not a successfully decoded screenshot.

The output file disappears

If you used OutputType.FILE, the returned file is temporary and is deleted when the JVM exits. Copy it to your intended destination while the process is running. If you used ImageIO.write, check the destination path and handle a false return value, which indicates that no appropriate writer was found.

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

The screenshot shows only part of the element

Element screenshot behavior can be browser- and implementation-dependent when the driver is not W3C-conformant. Selenium describes best-effort behavior in that case, including capture of either the entire content or only the visible portion. Confirm what the driver supports and whether the rendered element is clipped or outside the visible area.

The screenshot looks blank or stale

A screenshot reflects rendered pixels at capture time. Verify that the page and target element have reached the state you intend to capture before requesting the screenshot. Element lookup alone does not establish that asynchronous page content or images have finished rendering; add application-appropriate waits when necessary.

Version and compatibility note

The Selenium Java API documentation for TakesScreenshot and OutputType retrieved on September 29, 2026 does not establish a specific Selenium release number. Oracle’s cited ImageIO API documentation is for Java SE 26; check the API documentation matching the JDK actually used by your project before relying on version-specific behavior.

Frequently Asked Questions

Does a WebElement screenshot contain the element’s HTML?

No. It represents rendered image pixels; inspect the DOM separately through Selenium if you need markup or element attributes.

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

Can the result be sent as text?

Yes. Selenium’s OutputType.BASE64 returns Base64-encoded screenshot data.

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.