3

I tried the below code for taking full page screenshot. But only the visible area is captured,

public void Fullscreen (WebDriver driver) 
{
    try {
        final Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
        final BufferedImage image = screenshot.getImage();
        ImageIO.write(image, "PNG", new File("D:\\" + "AShot_BBC_Entire.png"));           
    } catch(Exception e){
        System.out.println(e.getMessage());
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ismail
  • 79
  • 2
  • 13

2 Answers2

7

While working with Selenium Java Client v3.14.0, ChromeDriver v2.41, Chrome v 68.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
  • I'm new to selenium, I need the same code you've written here. I stuck at running your code. It shows import error. I resolved all import statements except selenium.webdriver i.e., `import org.openqa.selenium.WebDriver;`. CAn you help me where can I get this package? – Mohamed Thasin ah Jul 26 '19 at 11:21
  • Exception in thread "main" javax.imageio.IIOException: Can't create an ImageOutputStream! at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1551) at WorldAirfaresTests.ashot_CompletePage.main(ashot_CompletePage.java:28) – Justin Lambert Sep 30 '20 at 06:06
  • @JustinLambert Let us discuss this in [Selenium Chat Room](https://chat.stackoverflow.com/rooms/223360/selenium) – undetected Selenium Oct 20 '20 at 14:15
  • Visiting this a few years later, it would be seen as better to use driver.close() than driver.quit() (until the end of your whole program) – dmanexe Dec 06 '20 at 12:17
3

Want to add answer in case when you don't know what kind of screen is used. (retina or not)

In this case you need find devicePixelRatio of browser window:

Object output = ((JavascriptExecutor) webDriver).executeScript("return window.devicePixelRatio");
String value = String.valueOf(output);
float windowDPR = Float.parseFloat(value);

Then you can use ShootingStrategy with scaling;

ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR), 100)
Degard
  • 192
  • 1
  • 7
  • 1
    It seems like latest versions of Ashot do not recognize the `int 100` parameter. I had to edit the code to: `ShootingStrategy shootingStrategy = ShootingStrategies.scaling(windowDPR);` – Gabriel Candia Oct 27 '21 at 14:28