For a Java screenshot, use Selenium’s TakesScreenshot if your project already runs WebDriver tests, or Playwright Java if you need explicit full-page capture, locator screenshots, in-memory bytes, and more control over image output. Selenium can save a driver or element screenshot to a file; Playwright can save viewport or full-page shots and return bytes directly. In either case, wait for the page state you need before capturing.
Choose Selenium or Playwright Java
Both libraries can capture a browser-rendered page, but they expose different levels of screenshot control. Selenium is the practical fit for an existing WebDriver stack. Playwright makes distinctions such as viewport versus full-page capture explicit and offers screenshot options for visual testing.
| Need | Selenium WebDriver | Playwright Java |
|---|---|---|
| Capture a page | TakesScreenshot on the driver |
Page.screenshot |
| Capture one element | WebElement.getScreenshotAs |
Locator.screenshot |
| Full-page capture | Coverage depends on the driver and implementation | Explicit with setFullPage(true) |
| Get image in memory | Choose an output type such as OutputType.BASE64 |
Returns a byte[] when no path is set |
| Formats and visual controls | Driver-dependent | PNG, JPEG and WebP, plus scale, masking, animation and timeout controls |
Selenium’s Java API describes TakesScreenshot as an interface for a driver or HTML element to capture a screenshot in different ways (Selenium TakesScreenshot API). Playwright’s Java screenshot API provides more screenshot-specific options (Playwright Page API).
Capture a screenshot with Selenium Java
After navigating, cast the driver to TakesScreenshot, request a temporary file, then copy it to the desired destination. The temporary-file approach avoids relying on where the browser driver happens to store its result.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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 browser session is closed in a finally block so it is shut down even if navigation or file writing fails. Replace the example URL and output path for your application.
Capture an element with Selenium
Find the element and call its screenshot method. For example, this captures a page header rather than the full browser view:
WebElement header = driver.findElement(By.cssSelector("header"));
File elementFile = header.getScreenshotAs(OutputType.FILE);
Use the same file-copy pattern as the page example to save elementFile at a known path. Selenium’s official browser documentation also demonstrates element-level capture (Selenium: take screenshot of an element).
Return Base64 instead of a file
If the next step needs a string—for example, embedding an image in a report—request Base64 output:
Rank #2
String screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.BASE64);
For binary processing, choose an output type that returns bytes if supported by your Selenium API and driver, rather than converting through Base64 unnecessarily.
Understand Selenium page coverage
A driver screenshot is not a guarantee of a full-length webpage image. For W3C-conformant implementations, Selenium follows the WebDriver screenshot behavior; otherwise it makes a best effort that may capture the entire page, the current window, the current frame, or the display. If exact full-page coverage matters, verify what your chosen browser driver returns or use Playwright’s explicit full-page option.
Capture a screenshot with Playwright Java
Playwright’s Java flow is to launch a browser, create a page, navigate, and call screenshot. The following saves a viewport screenshot to a file:
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();
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://example.com");
page.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get("page.png")));
browser.close();
}
}
}
This is the file-save pattern shown in the Playwright Java screenshot guide. The screenshot is a viewport capture unless you request full-page mode.
Capture the full scrollable page
Set fullPage to true when the output should include content beyond the current viewport:
page.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get("full-page.png"))
.setFullPage(true));
This is an explicit Playwright option. It is useful for page records or visual checks where a viewport-only image would omit lower sections.
Get a screenshot as a byte array
Omit the path and Playwright returns the screenshot bytes. You can pass the resulting array to an image-processing or storage layer without first writing a file:
byte[] png = page.screenshot();
Capture a locator or element
A locator screenshot clips the output to the matched element. Playwright scrolls the element into view and performs actionability checks before capture:
Rank #4
page.locator(".header").screenshot(
new Locator.ScreenshotOptions().setPath(Paths.get("header.png")));
Prefer a locator that uniquely identifies the intended element. If the locator matches nothing or more than one element, resolve that ambiguity before capturing.
Control image format and visual-test behavior
Playwright’s screenshot options cover the main variables that affect output and repeatability. Check the Page API for the exact option names and supported values for the Playwright version in your project.
- Format: PNG is the default; JPEG and WebP are available. Quality is relevant to lossy formats.
- Scale: CSS scale maps output pixels to CSS pixels; device scale captures at the device pixel ratio, which is useful for high-DPI evidence.
- Mask: Cover changing or sensitive regions such as timestamps, ads, or user-specific content so they do not create irrelevant visual differences.
- Animations: Control or disable animation when producing visual-test screenshots that need stable output.
- Timeout: Set a timeout appropriate to the page’s load and screenshot conditions; a timeout does not itself ensure the page has reached the right application state.
- Background and stylesheet controls: Use the available options when the screenshot should use a particular background or stylesheet behavior.
For visual comparisons, keep the viewport and browser/runtime consistent between runs. A change in viewport dimensions, device scale, loaded content, or animation state can change pixels even when the application code has not changed.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Wait for the right page state before capture
Navigation completing does not necessarily mean the content you want is ready. A page may still be loading data, rendering an image, or changing due to animation. Wait for the specific state your capture needs rather than taking a screenshot immediately after navigation.
Best Value
- For a page-level capture, wait until the key content is visible or the relevant application state is established.
- For an element capture, make sure the target exists and is in the state you want included.
- For repeatable visual tests, use a fixed viewport and consistent browser/runtime versions.
- Mask volatile content or control animations in Playwright when it is not part of what the test measures.
Do not use a long arbitrary delay as a substitute for knowing what readiness means on the page. If the target depends on application data, a meaningful selector or state is a more useful readiness condition than a screenshot taken after an assumed wait.
Common failures and fixes
- The Selenium code fails at the cast to
TakesScreenshot: The active driver does not expose that interface. Use a browser driver that supports screenshots, or use Playwright for that capture path. - The Selenium image shows only a viewport or frame: Driver screenshot coverage is implementation-dependent. Use Playwright
setFullPage(true)when you need explicit full-page capture. - The screenshot is blank or missing dynamic content: The page may not have reached the relevant state. Wait for the content or element needed by the capture before taking it.
- The element screenshot fails to locate the target: Check the CSS selector and ensure the element is present before calling the screenshot method. In Playwright, the locator must identify the intended element.
- The screenshot file is missing or at an unexpected path: Provide an explicit path and verify the process can write to its parent directory. With Selenium’s file output, copy the temporary file to the destination you control.
- Visual comparisons vary between runs: Stabilize the viewport and runtime, and control animations or mask volatile regions in Playwright.
- The Playwright screenshot call times out: Check whether the target page or locator is ready and whether the configured timeout fits the page’s behavior. Increasing the timeout will not correct a selector or readiness-condition error.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server from Yorker Media. One GET request returns a screenshot or PDF; it can also be used from an AI agent through its MCP server. For example, this cURL request saves a WebP screenshot:
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. Cookie banners and consent overlays, newsletter popups, and chat widgets are removed before capture; those cleanup steps can be disabled. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing result. An MCP server lets AI agents use screenshot tools. The free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000. Sign up free for ScreenshotNeo.
Frequently Asked Questions
Can Java save a screenshot without writing it to disk first?
Yes. Playwright’s page.screenshot() returns a byte[] when no output path is set. Selenium can return Base64 with OutputType.BASE64.
Recommended Free Tools
Which Java option is better for a full-page screenshot?
Playwright provides the explicit setFullPage(true) option. Selenium’s driver-level page coverage can vary by implementation.
Can I screenshot just one HTML element?
Yes. Selenium uses WebElement.getScreenshotAs; Playwright uses Locator.screenshot, which scrolls the matched element into view and captures its bounds.
Quick Recap
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.

