---Advertisement---

How to Take Screenshots in Selenium WebDriver: A Complete Guide Great 2025

By Manisha

Updated On:

---Advertisement---
How to Take Screenshots in Selenium

๐Ÿ‘‰How to Take Screenshots in Selenium WebDriver

How to Take Screenshots in Selenium WebDriver: Taking screenshots in Selenium WebDriver is a crucial step in automated testing and bug reporting. Screenshots help debug test failures, capture test evidence, and verify UI elements.

In this guide, you will learn:

โœ… How to take screenshots using Selenium WebDriver
โœ… How to capture full-page screenshots with Ashot API
โœ… How to save and store screenshots in a desired location
โœ… How to integrate screenshots into your test reports


๐Ÿ‘‰How to Take a Screenshot in Selenium WebDriver?

How to Take Screenshots in Selenium WebDriver: Selenium provides the TakeScreenshot interface, which allows you to capture screenshots.

javaTakesScreenshot screenshot = ((TakesScreenshot) driver);

Use the getScreenshotAs() method to capture the screenshot and store it as a file.

javaFile srcFile = screenshot.getScreenshotAs(OutputType.FILE);

Use the Apache Commons IO library to move the screenshot to a specific folder.

javaFile destFile = new File("C:\\Screenshots\\testScreenshot.png");
FileUtils.copyFile(srcFile, destFile);

๐Ÿ‘‰Full Example: Selenium WebDriver Screenshot Code

javaimport java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScreenshotExample {
public static void main(String[] args) throws IOException {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();

// Open Website
driver.get("https://www.example.com");

// Capture Screenshot
TakesScreenshot screenshot = ((TakesScreenshot) driver);
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);

// Save Screenshot to a specific location
File destFile = new File("C:\\Screenshots\\testScreenshot.png");
FileUtils.copyFile(srcFile, destFile);

System.out.println("Screenshot saved successfully!");

// Close the browser
driver.quit();
}
}

๐Ÿ‘‰What is AShot API?

How to Take Screenshots in Selenium WebDriver: AShot is a third-party Selenium utility that allows you to:
โœ… Take full-page screenshots (beyond viewport)
โœ… Capture specific WebElements
โœ… Handle zooming, scaling, and blurring

How to Install AShot API?

1๏ธโƒฃ Using Maven (Recommended)
Add the following dependency to your pom.xml file:

xml<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>

2๏ธโƒฃ Manually Download the AShot JAR


How to Take Screenshots in Selenium WebDriver: By default, TakeScreenshot only captures the visible portion of the page. To take a full-page screenshot, use AShot API.

javaimport java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

public class FullPageScreenshot {
public static void main(String[] args) throws IOException {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();

// Open the webpage
driver.get("https://www.example.com");

// Capture full-page screenshot
Screenshot screenshot = new AShot()
.shootingStrategy(ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);

// Save the screenshot
ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\Screenshots\\fullPageScreenshot.png"));

System.out.println("Full-page screenshot saved successfully!");

// Close the browser
driver.quit();
}
}

๐Ÿ‘‰How to Capture a WebElement Screenshot in Selenium?

How to Take Screenshots in Selenium WebDriver: AShot also allows you to capture a screenshot of a specific WebElement.

javaimport java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;

public class ElementScreenshot {
public static void main(String[] args) throws IOException {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();

// Open the webpage
driver.get("https://www.example.com");

// Locate the WebElement
WebElement element = driver.findElement(By.id("logo"));

// Capture WebElement Screenshot
Screenshot screenshot = new AShot().takeScreenshot(driver, element);

// Save the screenshot
ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\Screenshots\\elementScreenshot.png"));

System.out.println("Element screenshot saved successfully!");

// Close the browser
driver.quit();
}
}

๐Ÿ‘‰Comparison: TakeScreenshot vs. AShot API

FeatureTakeScreenshot (Selenium)AShot API (Third-Party)
Captures full-page screenshotโŒ Noโœ… Yes
Captures WebElement screenshotโŒ Noโœ… Yes
Supports zooming & scalingโŒ Noโœ… Yes
Requires external dependencyโŒ Noโœ… Yes
Works on all browsersโœ… Yesโœ… Yes

๐Ÿ‘‰Common Issues & Solutions in Selenium Screenshots

IssueSolution
Screenshot not savingEnsure the file path is correct. Use C:\\Screenshots\\ for Windows.
FileNotFoundExceptionAdd Apache Commons IO JAR for FileUtils.copyFile().
Full-page screenshot not workingUse AShot API instead of TakeScreenshot.
Element screenshot croppedEnsure the WebElement is fully visible before capturing.

๐Ÿ‘‰Conclusion

How to Take Screenshots in Selenium WebDriver: Taking screenshots in Selenium WebDriver is essential for debugging test failures and creating test reports.

๐Ÿ‘‰Key Takeaways:

โœ” How to Take Screenshots in Selenium WebDriver: Use TakeScreenshot for basic screenshots
โœ” Use AShot API for full-page and element-specific screenshots
โœ” Store screenshots in a structured directory for better test reporting
โœ” Integrate screenshots into TestNG Reports for better analysis

Start capturing Selenium screenshots today for better test debugging!

๐Ÿ‘‰Cross Browser Testing
๐Ÿ‘‰Testing Selenium Download 

Leave a Comment

Index