有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Selenium屏幕捕获图像不可用

我试图用selenium和firefox捕捉http://www.flipkart.comurl的屏幕

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) 个答案

  1. # 1 楼答案

    原因是页面正在通过Ajax加载图像。在做截图之前放一个Thread.sleep()。这不是一个好的解决方案,但它应该有效:)

  2. # 2 楼答案

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

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

    在截图之前添加这些行

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

    希望这能帮助你。。。如果您有任何疑问,请回电

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