1

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
roger_that
  • 9,493
  • 18
  • 66
  • 102

2 Answers2

16

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

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Serghei Mardar
  • 161
  • 1
  • 3
  • 1
    I needed to add a scaling strategy to the viewport pasting to get it to work, otherwise, it was getting cut off. Thank you for adding complementary useful info to the accepted answer. – George Pantazes Oct 04 '19 at 22:05
2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Tried some travel sites like booking.com/expedia.com and result is: vertically complete screenshot but horizontally half. – roger_that Jun 15 '18 at 16:47
  • `ShootingStrategies` is not available in 1.4.4 version but is available in 1.5.2 version of aShot. Am trying that only. – roger_that Jun 15 '18 at 16:51
  • @DebanjanB How can I screen a full vertical size with exactly horizontal size? i.e. 1920xFullSize, 1280xFullSize etc... – Evgenii Truuts Oct 08 '18 at 14:59
  • @EugeneTruuts The default strategy should work. Else can you raise a new question with your exact requirements so I can construct an answer for your question? – undetected Selenium Oct 08 '18 at 15:03
  • @DebanjanB https://stackoverflow.com/questions/52705346/how-can-i-screen-a-full-vertical-size-with-exactly-horizontal-size – Evgenii Truuts Oct 08 '18 at 15:17