How to Take a Screenshot in Selenium WebDriver Using Java?
Last Updated :
02 Aug, 2025
Selenium WebDriver is one of the most widely used tools for automating web applications. When conducting tests, debugging an issue, or simply capturing visual evidence of your tests, screenshots are an essential part of the process when the test cases fail or something issue occurs.
Firstly, set up Selenium WebDriver with Java. Here are the steps to perform how to take a screenshot in Selenium WebDriver:
Step 1. Setting Up Your Selenium Project
Create maven project and initialize the WebDriver into the BaseTest class.
BaseTest.java
Java
package io.learn;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class BaseTest {
protected WebDriver driver;
// Set up the ChromeDriver
@BeforeMethod
public void setup() {
// Set the path to your chromedriver executable
System.setProperty("webdriver.chrome.driver", "C:\\Users\\change the path for Chrome-Driver\\chromedriver.exe");
// Initialize the ChromeDriver
driver = new ChromeDriver();
}
// Close the browser after each test
@AfterMethod
public void teardown() {
if (driver != null) {
driver.quit();
}
}
}
This BaseTest class will initialize the ChromeDriver before each test and close the browser afterward.
Step 2: Capturing Screenshots in Selenium
There are two primary types of screenshots you might need to capture:
- Full Page Screenshot: Capture the entire page displayed in the browser.
- WebElement Screenshot: Capture a specific element on the page, such as a button, header, or form.
Step 3: Taking a Full Page Screenshot
To capture a full-page screenshot, we use the TakesScreenshot
interface provided by Selenium.
BasicScreenshotTest.java
Java
package io.learn.screenshots;
import io.learn.BaseTest;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.assertj.core.api.Assertions.assertThat;
public class BasicScreenshotTest extends BaseTest {
// Test for capturing a full page screenshot
@Test
void testCaptureScreenshot() throws IOException {
// Open the page
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
// Capture screenshot using WebDriver's TakesScreenshot interface
TakesScreenshot ts = (TakesScreenshot) driver;
File screenshot = ts.getScreenshotAs(OutputType.FILE); // Capture the screenshot as a file
System.out.println("Screenshot captured at: " + screenshot.getAbsolutePath());
// Define the destination path for saving the screenshot
Path destination = Paths.get("C:\\path to save the screenshot\\bonigarcia-screenshot.png");
// Move the screenshot to the desired location
Files.move(screenshot.toPath(), destination, REPLACE_EXISTING); // Move the screenshot file to the destination
System.out.println("Screenshot saved at: " + destination);
// Assert that the screenshot file exists at the destination
assertThat(destination).exists();
}
// Test for capturing screenshot of a specific WebElement
@Test
void testCaptureWebElementScreenshot() throws IOException {
// Open the page with a specific element to capture
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
// Find a specific web element (in this case, a header on the page)
WebElement header = driver.findElement(By.tagName("h1"));
// Capture screenshot of the specific web element
File screenshot = header.getScreenshotAs(OutputType.FILE); // Capture the screenshot of the header element
System.out.println("WebElement screenshot captured at: " + screenshot.getAbsolutePath());
// Define the destination path for saving the screenshot
Path destination = Paths.get("C:\\path to save the screenshot\\bonigarcia-web-element-screenshot.png");
// Move the screenshot to the desired location
Files.move(screenshot.toPath(), destination, REPLACE_EXISTING); // Move the screenshot file to the destination
System.out.println("WebElement screenshot saved at: " + destination);
// Assert that the screenshot file exists at the destination
assertThat(destination).exists();
}
}
Here we did:
- TakesScreenshot Interface: This interface allows you to capture a screenshot.
- File Handling: After capturing the screenshot, we move it to the desired directory.
- WebElement: In this example, we capture the header element (
<h1>
). You can replace it with any other element on the page, such as buttons or images. - The screenshot is captured only for the specified WebElement, not the entire page.
Step 4: Running the Tests
You can run them using TestNG or any other test runner that you prefer.
Alternatively, if you're using Maven, you can run the following command:
mvn test
Output:
Output of ScreenShot testStep 5: Validating and Handling Screenshots
We ensure that the screenshot file is saved correctly on the specific path:
1. bonigarcia-screenshot.png
output of entire screen screenshot2. bonigarcia-web-element-screenshot.png
Screenshot capture specific webelementBy saving these screenshots to the appropriate location, you can use them for visual validation, debugging, or documentation purposes.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING