How to get screenshot of full webpage using Selenium and Java?

SeleniumAutomation TestingTesting Tools

Whenever we encounter a failure during testing, it is a common nature to capture the screenshots wherever there is a deviation from the expected result. Thus it is considered a mandatory step to attach a screenshot for creating a bug.

While automating a bunch of test cases of a considerable number, capturing screenshot is critical to infer why a test case has failed for both the development and testing team. As they debug the failures, going through the screenshot and conclude if the failure is due to script issue or defect in the application.

Sometimes we may need to capture the screenshots of the full page and not only the viewable part of the screen. The latest versions of the common browsers mostly capture the area which is visible.

To solve this problem, we have to use AShot() method. It is a method provided by webdriver to get a full screen image and available from versions 3.x of Selenium. It comes with below usages −

  • Full screen image capture.

  • Enhance screenshot.

  • Comparison between screenshots.

We have to download and add the following jar to our project before using AShot() −

Syntax −

Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(s.getImage(),"PNG",new File("<< file path>>"));


Code Implementation.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
public class ScreenshotFull{
   public static void main(String[] args) {
      System.setProperty("", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "";
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // capture screenshot and store the image
      Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).take       Screenshot(driver);
      ImageIO.write(s.getImage(),"PNG",new File("tutorialspoint.png"));


Updated on 28-Aug-2020 12:22:43