selenium webdriver的屏幕截图问题



我有一个屏幕截图问题。当我捕获一个屏幕时,它只占用可见屏幕。我想要捕捉整个页面。下面是我的代码。

WebDriver webDriver=getCurrentWebDriver();
WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver);
WebDriverGUIUtility.captureScreenShot(webDriver);

@naveen,这通常发生在Chrome浏览器上。ChromeDriver能够截取可见部分的屏幕截图。因此,这里的概念是使用Java脚本执行器滚动页面并获取多个图像,然后将它们组合为单个图像。FirefoxDriver能够毫无问题地拍摄整个屏幕的图像。下面是一个例子

@Test(enabled=true)
public void screenShotExample() throws IOException{
    //WebDriver driver = new FirefoxDriver();
    System.setProperty("webdriver.chrome.driver", "yourpath to chromeDriver\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.w3schools.com/");
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    JavascriptExecutor jexec = (JavascriptExecutor)driver;
    jexec.executeScript("window.scrollTo(0,0)"); // will scroll to (0,0) position 
    boolean isScrollBarPresent = (boolean)jexec.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight");
    long scrollHeight = (long)jexec.executeScript("return document.documentElement.scrollHeight");
    long clientHeight = (long)jexec.executeScript("return document.documentElement.clientHeight");
    int fileIndex = 1;
    if(driver instanceof ChromeDriver){
        if(isScrollBarPresent){
            while(scrollHeight > 0){
                File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
                jexec.executeScript("window.scrollTo(0,"+clientHeight*fileIndex++ +")");
                scrollHeight = scrollHeight - clientHeight;
            }
        }else{
            File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
        }
    }else{
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
    }
    // Combine all the .jpg file to single file
    driver.close();
    driver.quit();
}

要合并所有图像文件,您可以在这里找到一些帮助。

如果你使用maven,那么你可以使用shot来完成你的任务。为此,您需要在pom文件中添加依赖项:

<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.2</version>
</dependency>

并按如下方式使用代码片段:

Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(augmentedDriver);
ImageIO.write(screenshot.getImage(), "PNG", new File("d:\tmp\results.png"));

但是,如果您不使用maven,那么下载ashot jar(版本:1.5.2)文件并将其添加到构建路径中。这是你的帮助链接:https://javalibs.com/artifact/ru.yandex.qatools.ashot/ashot

希望这对你有帮助。

在Selenium 4中,FirefoxDriver提供了一个getFullPageScreenshotAs方法来处理垂直和水平滚动,以及固定元素(例如导航条)。其他库不需要。

System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
final FirefoxOptions options = new FirefoxOptions();
// set options...
final FirefoxDriver driver = new FirefoxDriver(options);
driver.get("https://stackoverflow.com/");
File fullScreenshotFile = driver.getFullPageScreenshotAs(OutputType.FILE);
// File will be deleted once the JVM exits, so you should copy it

下面是selenium的测试代码,用于截取google页面截图

public class ScreenShot {
    public WebDriver d;
    Logger log;
    @Test
    public void m1() throws Exception
    {   
        try
        {
            d=new FirefoxDriver();
            d.manage().window().maximize();
            d.get("https://www.google.co.in/?gfe_rd=cr&ei=4caQV6fxNafnugTjpIGADg");
            String pagetitle=d.getTitle();
            log = Logger.getLogger(FirefoxDriver.class.getName());
            log.info("logger is launched..");
            log.info("Title name : "+pagetitle);
            d.findElement(By.id("testing")).sendKeys("test");
            d.findElement(By.cssSelector("input.gsfi")).sendKeys("gmail account");
        }
        catch(Exception e)
        {
            System.out.println("something happened, look into screenshot..");
            screenShot();
        }
    }
    public void screenShot() throws Exception
    {
        Files.deleteIfExists(Paths.get("G:\"+"Test results.png"));
        System.out.println("previous pics deleted...");
        File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile,new File("G:\"+"Test results.png"));
    }

}

最新更新