Selenium Take Full Page Screenshot

Profile picture for user arilio666

Taking a full screenshot of a page in selenium java is not possible, but we can get around a jar file which might help us take a beautiful screenshot of the whole page.

To do this, we need to use a third-party library called Ashot. Taking a screenshot using this is flawless and has more capability options than the original screenshot capability from selenium.

1.) POM

<dependency>
   <groupId>ru.yandex.qatools.ashot</groupId>
   <artifactId>ashot</artifactId>
   <version>1.5.3</version>
</dependency>
  • Add this XML to your pom.xml file, and this will install all the necessary jars for ashot to take over.

2.) Example

ru.yandex.qatools.ashot.Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
  • This is how we need to initiate Ashot and set the shooting strategy to the viewport value of 1000 and then take screenshot options are passed with the casting of the driver.
ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\TestLeaf\\fullPage.png"));
  • We are using the ImageIO to write the screenshot after getting the image and fixing the extension along with the path for storing the file.
    WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.programsbuzz.com/");
        driver.manage().window().maximize();
        ru.yandex.qatools.ashot.Screenshot screenshot = new AShot()
                .shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
        try {
            ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\TestLeaf\\fullPage.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
  • Here we can see the full-view screenshot taken by Ashot.