---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