【问题标题】:Capture screenshot of all the elements present on a page捕获页面上所有元素的屏幕截图
【发布时间】:2016-09-21 20:52:00
【问题描述】:

我正在尝试捕获网页上所有元素的屏幕截图,并希望将其存储在我编写了以下代码的磁盘中。

唯一的问题是这段代码只适用于第一次迭代,之后会发生一些意想不到的事情。

List<WebElement> eleId = driver.findElements(By.xpath("//*[@id]")); //fetch all the elements with ID attribute
    System.out.println(eleId.size());

    for (int i = 0;i < eleId.size();i++) {

//          Get entire page screenshot
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            BufferedImage fullImg = ImageIO.read(screenshot);

//          Get the location of element on the page
            Point point = eleId.get(i).getLocation();

//          Get width and height of the element
            int eleWidth = eleId.get(i).getSize().getWidth();
            int eleHeight = eleId.get(i).getSize().getHeight();

//          Crop the entire page screenshot to get only element screenshot
            BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
            ImageIO.write(eleScreenshot, "png", screenshot);

//          Creating variables name for image to be stores in the disk
            String fileName = eleId.get(i).getAttribute("id");
            String imageLocation = "D:\\" + fileName + ".png";
//          System.out.println(imageLocation);

//          Copy the element screenshot to disk
            File screenshotLocation = new File(imageLocation);
            FileUtils.copyFile(screenshot, screenshotLocation);

            System.out.println("Screenshot has been stored.");
}

【问题讨论】:

  • 意料之外的事情是什么意思?你的脚本有什么错误吗?
  • 它不会抛出任何错误,而是只运行一次迭代。

标签: selenium selenium-webdriver screenshot bufferedimage


【解决方案1】:

嗨 Sandeep 试试下面的代码。它对我有用。我只是添加了一个“if”条件来检查图像的高度和宽度。

@Test(enabled=true)
public void getIndividualElementScreenShot() throws IOException{
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/");
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    List<WebElement> eles = driver.findElements(By.xpath("//*[@id]"));
    System.out.println(eles.size());
    for(WebElement ele : eles){
        //Get Entire page screen shot
        File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        BufferedImage fullImage = ImageIO.read(screenShot);

        //Get the location on the page
        Point point = ele.getLocation();

        //Get width and height of an element
        int eleWidth = ele.getSize().getWidth();
        int eleHeight = ele.getSize().getHeight();

        //Cropping the entire page screen shot to have only element screen shot
        if(eleWidth != 0 && eleHeight != 0){
            BufferedImage eleScreenShot = fullImage.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
            ImageIO.write(eleScreenShot, "png", screenShot);


            //Creating variable name for image to be store in disk
            String fileName = ele.getAttribute("id");
            String imageLocation = "F:\\ElementImage\\"+fileName+".png";
            System.out.println(imageLocation);

            //Copy the element screenshot to disk
            File screenShotLocation = new File(imageLocation);
            org.apache.commons.io.FileUtils.copyFile(screenShot, screenShotLocation);

            System.out.println("Screen shot has beed stored");

        }
    }
    driver.close();
    driver.quit();
}

【讨论】:

  • Ty @Sandipan Pramanik,我认为问题在于我试图访问的元素的宽度和高度...tysm :)
【解决方案2】:

您的代码运行良好。但是您在每次迭代中都覆盖了文件。你明白了吗?通过在 fileName 变量中分配新文件名来更改每次迭代中的文件名。使用下面的代码:

        List<WebElement> eleId = driver.findElements(By.xpath("//*[@id]")); //fetch all the elements with ID attribute
        System.out.println(eleId.size());

        for (int i = 0;i < eleId.size();i++) {

//            Get entire page screenshot
                File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                BufferedImage fullImg = ImageIO.read(screenshot);

//            Get the location of element on the page
                Point point = eleId.get(i).getLocation();

//            Get width and height of the element
                int eleWidth = eleId.get(i).getSize().getWidth();
                int eleHeight = eleId.get(i).getSize().getHeight();

//            Crop the entire page screenshot to get only element screenshot
                BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
                ImageIO.write(eleScreenshot, "png", screenshot);

//            Creating variables name for image to be stores in the disk
                String fileName = eleId.get(i).getAttribute("id");
                String imageLocation = "D:/" + fileName + i + ".png";
//            System.out.println(imageLocation);

//            Copy the element screenshot to disk
                File screenshotLocation = new File(imageLocation);
                FileUtils.copyFile(screenshot, screenshotLocation);

                System.out.println("Screenshot has been stored.");
    }

【讨论】:

  • 我也试过了,但输出还是一样,第一次迭代后就停止了。
  • 复制并粘贴已编辑答案中的代码部分。我想,这会有所帮助。 :)
  • 也尝试过复制粘贴..但只有一次迭代。
  • 那么这个路径定位器只有一个元素。它适用于页面中的多个元素。希望你明白了。
  • 您可以通过将您的代码替换为提供的代码来再次检查它。让我知道它是否有效。我想,应该的。我自己测试过了。
猜你喜欢
  • 1970-01-01
  • 2015-04-03
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多