如何通过Selenium和Java使用AShot库获取完整的屏幕截图

时间:2018-08-17 10:16:49

标签: java selenium webdriver screenshot ashot

我尝试了以下代码以截取全屏截图。但是只有可见区域被捕获了,

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());
    }
}

2 个答案:

答案 0 :(得分:3)

使用 ashot-1.4使用 Selenium Java Client v3.14.0 ChromeDriver v2.41 Chrome v 68.0 时。 4.jar 这是一个使用 ChromeDriver aShot Library 完整页面屏幕截图水平和垂直拍摄的示例em> url https://jquery.com/

  • 代码块:

    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();
        }
    }
    
  • 屏幕截图:

screenshot

答案 1 :(得分:0)

如果您不知道使用哪种屏幕,想添加答案。 (是否是视网膜)

在这种情况下,您需要找到浏览器窗口的devicePixelRatio:

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

然后,您可以缩放使用ShootingStrategy;

ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR), 100)