Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:SeleniumHQ Selenium TakesScreenshot

From Leeroopedia
Knowledge Sources
Domains WebDriver, Browser_Automation
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for capturing browser or element screenshots provided by the Selenium WebDriver library.

Description

The TakesScreenshot interface indicates that a WebDriver instance or a WebElement can capture a screenshot and return it in a specified format. It defines a single method, getScreenshotAs(), which accepts an OutputType<X> parameter to control the return format. For W3C-conformant drivers, the behavior follows the W3C WebDriver screen capture specification; for non-conformant drivers, it makes a best effort to capture the entire page, current window, visible frame portion, or entire display.

Usage

Import TakesScreenshot when you need to capture visual snapshots of the browser viewport or individual HTML elements during test execution, typically for debugging failures, generating visual regression baselines, or producing test reports.

Code Reference

Source Location

Signature

public interface TakesScreenshot {
    <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException;
}

Import

import org.openqa.selenium.TakesScreenshot;

I/O Contract

Inputs

Name Type Required Description
target OutputType<X> Yes The desired output format for the screenshot. Use OutputType.FILE, OutputType.BASE64, or OutputType.BYTES.

Outputs

Name Type Description
getScreenshotAs result X The screenshot in the format specified by the OutputType parameter: a File, a base64 String, or a byte array.

Usage Examples

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

WebDriver driver = // ... obtain driver instance

// Capture a full-page screenshot as a file
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Files.copy(screenshot.toPath(),
    new File("full_page.png").toPath(),
    StandardCopyOption.REPLACE_EXISTING);

// Capture a screenshot as a base64-encoded string for HTML embedding
String base64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
String imgTag = "<img src=\"data:image/png;base64," + base64 + "\" />";

// Capture an element-level screenshot
WebElement logo = driver.findElement(By.id("logo"));
File elementScreenshot = ((TakesScreenshot) logo).getScreenshotAs(OutputType.FILE);
Files.copy(elementScreenshot.toPath(),
    new File("logo_element.png").toPath(),
    StandardCopyOption.REPLACE_EXISTING);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment