Selenium: Not able to take complete page screenshot using aShot library

8.2k Views Asked by At

Am trying to take the complete page screenshot both horizontally and vertically using Firefox gecko driver and aShot Library.

However, the results are not as expected. Take a look:

enter image description here

driver.get("https://google.com");

Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"JPEG",new File("FullPageScreenshot.jpg"));

Looked into a lot of variants but nothing is working. Interestingly, when I try using old firefox version (46), I am able to take full screenshot without any third party library. Am trying to use latest firefox and have full screenshot functionality.

Any help?

2

There are 2 best solutions below

5
On

While working with Selenium Java Client v3.12.0, ChromeDriver v2.40, Chrome v 67.0 using ashot-1.4.4.jar here is an example to take the complete page screenshot both horizontally and vertically using ChromeDriver and aShot Library of the url https://jquery.com/:

  • Code Block:

    import java.io.File;
    import javax.imageio.ImageIO;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    import ru.yandex.qatools.ashot.AShot;
    import ru.yandex.qatools.ashot.Screenshot;
    import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
    
    public class ashot_CompletePage {
    
        public static void main(String[] args) throws Exception {
    
            System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            options.addArguments("--disable-extensions"); 
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://jquery.com/");
            new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
            Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
            ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
            driver.quit();
        }
    }
    
  • Screenshots:

screenshot


Reference

You can find a detailed discussion in How to take screenshot with Selenium WebDriver

1
On

Try:

Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(1.75f), 1000)).takeScreenshot(driver);

where 1.75f is device pixel ratio (you can run window.devicePixelRatio; in browser console to find it). If it's still not capturing full screen, change it to 2f