【问题标题】:Screen shot issue in selenium webdriverselenium webdriver中的屏幕截图问题
【发布时间】:2017-02-12 03:39:01
【问题描述】:

我遇到了屏幕截图问题。当我捕获屏幕时,它只需要可见屏幕。我想捕获整个页面。下面是我的代码。

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

【问题讨论】:

  • 您使用的是哪个驱动程序(浏览器)?
  • 我正在使用 mozilla

标签: selenium


【解决方案1】:

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

要合并所有图像文件,您会找到一些帮助here。希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

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

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

    并使用代码sn-p如下:

    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

    希望对你有帮助。

    【讨论】:

    • 你好老板ViewportPastingStrategy(1000)是什么意思
    • naveen viewpastingstrategy 表示页面向上滚动 1000 毫秒,直到页脚结束
    【解决方案3】:

    在 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
    

    【讨论】:

      【解决方案4】:

      下面是 selenium 和 testng 代码来获取谷歌页面截图

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

      }

      【讨论】:

      • 该建议包含大量与原问题截图无关的代码,也没有解决原问题对整个页面(不仅仅是可见部分)截图的问题
      猜你喜欢
      • 2014-08-25
      • 2015-10-28
      • 1970-01-01
      • 2014-07-15
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多