2

我尝试了下面的代码来获取整页截图。但只捕获可见区域,

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

2 回答 2

7

在使用Selenium Java Client v3.14.0ChromeDriver v2.41Chrome v 68.0使用ashot-1.4.4.jar 时,这里是一个使用ChromeDriverurlaShot 库水平和垂直截取完整页面截图的示例: 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();
        }
    }
    
  • 截图:

截屏


参考

您可以在如何使用 Selenium WebDriver 截屏中找到详细讨论

于 2018-08-17T10:58:58.223 回答
2

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

在这种情况下,您需要找到浏览器窗口的 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)
于 2020-11-25T11:51:18.600 回答