【问题标题】:Selenium Screen Capture - Image UnavailableSelenium 屏幕捕获 - 图像不可用
【发布时间】:2015-07-21 10:09:16
【问题描述】:

我正在尝试使用 selenium 和 firefox 捕获 http://www.flipkart.com url 的屏幕。

public class App {

    private static final String APP_URL = "http://www.flipkart.com";

    public static void main(String[] args) {
        WebDriver webDriver = null;
        try {
            webDriver = new FirefoxDriver();
            webDriver.get(APP_URL);
            webDriver.manage().window().maximize();

            if (webDriver instanceof TakesScreenshot) {
                TakesScreenshot screenshot = (TakesScreenshot) webDriver;
                File imageFile = screenshot.getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(imageFile, new File(
                        "C:\\Captures\\captured.png"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (webDriver != null) {
                webDriver.quit();
            }
        }
    }
}

它需要整个页面的屏幕截图,但内页显示图像对于许多其他图像不可用。我无法纠正它。帮帮我。

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    最好的办法是滚动页面然后截屏

      //scroll to the bottom of the page
     ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
     ////scroll to the top of the page
     ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,0)");
    

    在截屏前添加这些行

    我尝试了上面的解决方案,效果很好

    希望这对您有所帮助...如果您有任何疑问,请回复

    【讨论】:

    • 效果很好,页面滚动时似乎正在加载图像
    【解决方案2】:

    原因是页面正在通过 Ajax 加载图像。在制作屏幕截图之前输入Thread.sleep()。这不是一个很好的解决方案,但它应该可以工作:)

    【讨论】:

      【解决方案3】:

      使用该功能对打开的页面进行抓图,并放置在对应的文件夹路径中。但所有的图片都以png格式存储。

       public static void captureScreenShot(WebDriver ldriver) {
      
              // Take screenshot and store as a file format
              File src = ((TakesScreenshot) ldriver).getScreenshotAs(OutputType.FILE);
              try {
                  // now copy the screenshot to desired location using copyFile method
                  // title=ldriver.getTitle();
                  FileUtils.copyFile(src, new File(System.getProperty("user.dir") + "\\src\\data\\" + siteCapture + "\\"
                          + System.currentTimeMillis() + ".png"));
              }
      
              catch (IOException e)
      
              {
      
                  System.out.println(e.getMessage());
      
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 2011-10-13
        • 2013-06-16
        • 2012-01-20
        • 1970-01-01
        • 2018-12-03
        • 2010-12-04
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        相关资源
        最近更新 更多