In Automation, it is advisable to take screenshots of failed test cases for further analysis and proof of failure. Selenium provides the capability to take screenshot. But, before we see how to capture Screenshot in Selenium, we need to add below mentioned dependencies in the Maven project. In this tutorial, we will take screenshot of a specific element in the webpage.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
Implementation Steps
1. Initialize WebDriver
Initialize the WebDriver. Here, it is FireFoxOptions. Open the desired web page.
FirefoxOptions options = new FirefoxOptions();
WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/");
2. Locate the Element
Use Selenium methods to locate the element.
WebElement logo = driver.findElement(By.xpath("//div[@class='row']/div"));
3. Take the screenshot of the element
To capture a screenshot in Selenium, we can make use of an interface, called TakesScreenshot. This method indicates the driver, that it can capture a screenshot and store it in different ways
File source = logo.getScreenshotAs(OutputType.FILE);
In order to capture screenshot and store it in a particular location, there is a method called “getScreenshotAs“, where OutputType defines the output type for a screenshot.
4. Specify the location to save the screenshot
File screenshotLocation = new File("src//test/resources//screenshot/specific_element_screenshot.png");
5. Save the screenshot to the specified location
We want to save the element in the screenshot folder present in src/test/resources directory. So, copy the screenshot in that folder.
FileUtils.copyFile(source, screenshotLocation);
5. Quit the browser
Make sure to quit the browser to free up all the resources.
driver.quit();
Let’s see the complete program
package com.example;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ScreenShot_Demo {
public static void main(String[] args) {
FirefoxOptions options = new FirefoxOptions();
WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/");
// Locate the specific element
WebElement logo = driver.findElement(By.xpath("//div[@class='row']/div"));
// Take the screenshot of the element
File source = logo.getScreenshotAs(OutputType.FILE);
// Specify the location to save the screenshot
File screenshotLocation = new File("src//test/resources//screenshot/element_screenshot.png");
try {
// Save the screenshot to the specified location
FileUtils.copyFile(source, screenshotLocation);
System.out.println("Screenshot saved to: " + screenshotLocation.getAbsolutePath());
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("The Screenshot is taken and saved under Screenshots folder");
driver.quit();
}
}
The output of the above program is

A folder with name screenshot is created inside src/test/resources directory and the screenshot is placed in that folder as you can see the image below

The Screenshot looks like something below

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!




















