Selenium屏幕捕获 - 图像不可用

时间:2015-07-21 10:09:16

标签: java selenium selenium-webdriver

我正在尝试使用硒与Firefox一起捕捉http://www.flipkart.com网址的屏幕。

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

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

Screen Shot Taken With Selenium

3 个答案:

答案 0 :(得分:6)

最好的解决方案是滚动页面,然后截取屏幕截图

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

在截屏之前添加这些行

我尝试了上面的解决方案,它工作正常

希望这可以帮助你...如果您有任何疑问,请回来

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用此功能捕获已打开页面的图像,并放置相应的文件夹路径。但所有图像都以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());

        }

    }
相关问题