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

For a basic screenshot in Java, use Selenium’s TakesScreenshot with a WebDriver. For explicit full-page capture, image-format options, in-memory bytes, or reliable element screenshots, Playwright Java provides more screenshot-specific controls. In either case, wait for the page state you need before capturing: a successful navigation does not guarantee that late-loading content is ready.

Choose Selenium or Playwright Java

Use Selenium if your project already runs browser automation through WebDriver. Use Playwright Java when you want an explicit full-page option and controls such as image format, output scale, masking, animation handling, and locator screenshots.

Need Selenium WebDriver Playwright Java
Capture a page or driver TakesScreenshot Page.screenshot
Capture an element WebElement.getScreenshotAs Locator.screenshot
Full-page capture Coverage depends on the driver and implementation. Explicitly enable with setFullPage(true).
Return image data in memory Use an output type such as OutputType.BASE64. Call page.screenshot() to receive a byte[].
Screenshot controls Primarily browser and driver configuration. PNG, JPEG and WebP; scale, masking, animation handling, quality, timeout and background options.

Neither approach can guarantee that a screenshot is meaningful unless the page has reached the state your task requires. The examples below use Chromium-based browsers; Selenium’s exact screenshot coverage remains implementation-dependent.

Capture a screenshot with Selenium Java

Selenium’s TakesScreenshot interface captures a screenshot to different output types. The following standalone example navigates to a page and saves its image as page.png:

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.io.File;
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;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumScreenshot {
  public static void main(String[] args) throws Exception {
    WebDriver driver = new ChromeDriver();
    try {
      driver.get("https://example.com");
      File tmp = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
      Files.copy(tmp.toPath(), Path.of("page.png"),
          StandardCopyOption.REPLACE_EXISTING);
    } finally {
      driver.quit();
    }
  }
}

The temporary screenshot file returned by getScreenshotAs is copied to a path you choose. The finally block ensures the browser is closed even if navigation or file handling fails. This example requires Selenium and a usable Chrome/ChromeDriver setup in your environment.

Capture one element

Find the target element, then call its screenshot method. Selenium documents screenshot capture for both drivers and HTML elements:

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.OutputType;

WebElement header = driver.findElement(By.cssSelector("header"));
File elementFile = header.getScreenshotAs(OutputType.FILE);

Use a selector that identifies one element on the current page. If the selector matches nothing, lookup fails before a screenshot is produced. Selenium’s official browser documentation shows this element-level pattern at Selenium: Take Screenshot.

Return Base64 instead of a temporary file

If the next step needs text rather than a file, request Base64 output:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
String screenshotBase64 = ((TakesScreenshot) driver)
    .getScreenshotAs(OutputType.BASE64);

The string contains encoded image data. Decode it before writing the image bytes to disk or passing them to a consumer that expects binary image data.

Understand Selenium’s page coverage

Selenium’s Java API describes TakesScreenshot as an interface for a driver or HTML element to capture a screenshot and store it in different ways. For W3C-conformant implementations, capture follows the WebDriver specification; otherwise, Selenium makes a best effort. Depending on the implementation, that result may cover the entire page, the current window, the current frame, or the display. Do not treat a driver screenshot as a portable full-page capture unless you have confirmed the behavior of your specific browser and driver. See the Selenium TakesScreenshot Java API.

Capture a screenshot with Playwright Java

Playwright’s Java API can save a page screenshot directly to a path. This example opens Chromium, navigates to a page, saves page.png, and closes the browser:

import com.microsoft.playwright.*;
import java.nio.file.Paths;

public class PlaywrightScreenshot {
  public static void main(String[] args) {
    try (Playwright pw = Playwright.create()) {
      Browser browser = pw.chromium().launch();
      try {
        BrowserContext context = browser.newContext();
        Page page = context.newPage();
        page.navigate("https://example.com");
        page.screenshot(new Page.ScreenshotOptions()
            .setPath(Paths.get("page.png")));
      } finally {
        browser.close();
      }
    }
  }
}

The Playwright Java guide documents this navigation-and-save flow. Install the Playwright Java dependency and its required browser binaries as described in the Playwright Java screenshot guide.

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.

Capture the full scrollable page

Set fullPage to true to capture the full scrollable page rather than only the viewport:

page.screenshot(new Page.ScreenshotOptions()
    .setPath(Paths.get("full-page.png"))
    .setFullPage(true));

This is the clearest choice when the requirement specifically says full-page screenshot. Very long pages can produce large image files; if your downstream system has size limits, consider capturing a specific element or using PDF instead.

Keep the screenshot in memory

Omit the path and Playwright returns screenshot bytes:

byte[] png = page.screenshot();

You can then store the byte array, send it to another service, or write it to a file with Java’s file APIs. The default screenshot format is PNG unless configured otherwise.

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

Capture a locator or element

A locator screenshot clips to the matched element, scrolls it into view, and runs actionability checks before capture:

import com.microsoft.playwright.Locator;
import java.nio.file.Paths;

page.locator(".header").screenshot(
    new Locator.ScreenshotOptions()
        .setPath(Paths.get("header.png")));

This is useful when you need a component image rather than a screenshot of the entire browser page. The locator must resolve to the intended element; an ambiguous or missing match should be corrected in the selector or page setup.

Set format, scale and visual-stability options

Playwright’s Page.ScreenshotOptions exposes controls for screenshot output, including PNG, JPEG and WebP, CSS or device scale, masks, animation settings, quality, timeout and background handling. Check the Page screenshot API for the exact Java option names and behavior for your installed version.

Choose CSS scale or device scale

Use CSS scale when you want output pixels to correspond to CSS pixels. Use device scale when a higher-DPI image is needed. For visual comparisons, keep the scale and viewport consistent between runs; otherwise, differences in dimensions can obscure the page changes you are trying to detect.

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

Reduce noise in visual tests

Animations, timestamps, advertisements and other changing regions can make otherwise identical screenshots differ. Playwright supports animation handling and masking, so disable or mask known dynamic regions when those differences are irrelevant to the test. Keep masks targeted: hiding too much can also hide a real regression.

Wait for meaningful content

Navigation completion is not the same as application readiness. Before capturing, wait for a locator or other condition that reflects the content your test actually needs. For example, a page with a results table should be captured after the table appears, not merely after the initial document loads. Prefer a fixed viewport and consistent browser/runtime versions for repeatable comparisons.

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server for developers. It accepts one GET request with a URL and returns an image or PDF. Its capture steps can accept cookie/consent banners and remove more than 60 known consent platforms, newsletter popups and chat widgets; each step can be turned off. Bot checks/CAPTCHAs, blank pages, timeouts, failed loads and cache hits cost nothing, with the response identifying the page verdict and billing status in headers. An MCP server provides screenshot tools for Claude, Cursor and other MCP clients.

For the API key and complete parameter reference, see the ScreenshotNeo documentation. Replace the example target URL with the page you want to capture:

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://example.com 
  -o shot.webp

ScreenshotNeo’s options include full-page capture with lazy images loaded, CSS-selector element capture, dark mode, device presets and custom viewports, retina scale, PDF settings, custom CSS and JavaScript, click-before-capture, wait conditions, request blocking, custom headers/cookies/user agent/Authorization, timezone and geolocation, transparency, resizing, caching, signed image links, asynchronous jobs, bulk capture and a usage API. Its request accepts parameter names used by other screenshot APIs to make switching easier.

The free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 screenshots; yearly billing gives two months free, and every feature is on every plan. Sign up for ScreenshotNeo’s free plan.

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

Troubleshoot common screenshot problems

The output shows only the visible area

With Selenium, full-page coverage is implementation-dependent, so the result may be limited to the current window or frame. If full-page output is a requirement, use Playwright’s explicit setFullPage(true) option or verify your Selenium implementation’s behavior.

The screenshot is blank or missing expected content

The capture may have happened before the application finished rendering, or the content may be inside a frame or loaded only after scrolling. Wait for the specific element or state you need, and verify that it is present before capture.

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

An element screenshot fails

Confirm the selector matches an element on the current page and that navigation has completed. Playwright locator screenshots scroll the element into view and perform actionability checks; Selenium requires that your element lookup succeeds before calling getScreenshotAs.

The image differs between runs

Dynamic content, viewport changes, browser/runtime differences and device scale can all affect output. Standardize the viewport and runtime, wait for stable content, and use Playwright’s animation and masking controls for expected changes such as timestamps.

The Java code cannot launch a browser

Check that the automation dependency is on the project classpath and that the browser required by the chosen library is installed or available to it. For Playwright, follow its Java installation instructions for browser binaries; for Selenium, confirm the Chrome/ChromeDriver setup is usable in the execution environment.

Which Java screenshot method should you use?

For an existing WebDriver test suite, Selenium’s TakesScreenshot is the shortest path to a page or element image. If full-page capture, in-memory output, format selection or screenshot-specific stability controls matter, Playwright Java provides more explicit tools. In both cases, make readiness and repeatability part of the capture logic rather than relying on navigation alone.

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

Frequently Asked Questions

Can Selenium Java take a full-page screenshot?

Sometimes, but the result depends on the browser driver and implementation; Selenium’s screenshot API does not make full-page coverage universally explicit.

Does Playwright Java return a screenshot as bytes?

Yes. Calling page.screenshot() without a path returns a byte[].

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.