屏幕截图:RasterFormatException(y 高度)在 Raster 之外

Screenshot : RasterFormatException (y+ height) is outside Raster

按照其他 post 的建议,使用 Selenium Webdriver 为特定 UI 元素截取屏幕截图,我正在使用 getSubimage 方法来捕获屏幕元素。但是,接收失败异常。

我不确定 uielement 的 getLocation().getX() 和 getSize().getWidth().getX() 的区别。如果有人可以澄清这一点,就像在 WritableRaster 方法的条件中一样,它总是检查 y+height

File imgSrc=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        Point point = uiElem.getLocation();    
         Point bottomright = new Point(uiElem.getSize().getWidth(),
                 uiElem.getSize().getHeight());             
          int xcord = point.getX();
          int ycord = point.getY();  

          BufferedImage img;
        try {
            img = ImageIO.read(imgSrc);
             BufferedImage dest = img.getSubimage(xcord, ycord, bottomright.getX(), bottomright.getY());
              ImageIO.write(dest, "png", imgSrc);       
              FileUtils.copyFile(imgSrc, new File(".\Screenshots\123.png"));          
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

调试时,我注意到 img(宽度 1366 和高度 = 613)。 getSubImage() 有 (194,1335,960,15)。对于 createWritableChildMethod 中的条件 (y+ height) >(this.minY + this.height ),它将始终失败。所以,任何人都可以指出哪里出了问题,也没有意义,因为为什么我们添加 (y+height)of sub-image 更大?

找到了一个解决方法,只是注意到最好直接在 UIElement 上使用 getScreenshotAs() 而不对元素进行大量操作。

public void takeSmallScreenshot(WebDriver driver, WebElement uiElem)
{
         File uiElementSrc = uiElem.getScreenshotAs(OutputType.FILE);
         try {
            FileUtils.copyFile(uiElementSrc, new File(".\Screenshots\"+uiElem.getText().substring(0,5)+".png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}