硒:java.awt.image.RasterFormatException:(Y +高度)是使用元素的大小和位置时光栅的外

parsecer:

我有我从拍了代码教程。它关系到一个learn-selenium-easy.blogspot.com网站,看上去对于大多数读/热门帖子部分,并采取它的照片:

 class SshotofElement {

    public static void screenShotElement() throws InterruptedException,IOException {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("marionette", true);
        WebDriver driver = new ChromeDriver(capabilities);

        driver.get("http://learn-selenium-easy.blogspot.com/");
        driver.manage().window().maximize();

        // Xpath of element to take screen shot
        WebElement element=driver.findElement(By.xpath("//*[@id='PopularPosts1']"));
        System.out.println(element.getSize());
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        // Take full screen screenshot
        BufferedImage  fullImg = ImageIO.read(screenshot);
        Point point = element.getLocation();
        int elementWidth = element.getSize().getWidth();
        int elementHeight = element.getSize().getHeight();
        BufferedImage elementScreenshot= fullImg.getSubimage(point.getX(), point.getY(), elementWidth,elementHeight);  //exception here

        // crop the image to required
        ImageIO.write(elementScreenshot, "png", screenshot);
        FileUtils.copyFile(screenshot, new File("mostread_screenshot.png"));//path to save screen shot

        driver.close();
    }
}

我得到一个java.awt.image.RasterFormatException: (y + height) is outside of Raster例外,但上线BufferedImage elementScreenshot= fullImg.getSubimage(point.getX(), point.getY(), elementWidth,elementHeight);我不知道为什么会是这样,由于图像的点和尺寸从元素本身所。

阿米特耆那教:
  1. 您正在裁剪要素图像不存在于由代码所采取的屏幕截图。如果你把调试并打印全屏幕截图路径和手动查看它,那么你可以看到所需的元素从图像裁剪是不存在的。

  2. 所以首先我们需要滚动页面,使所需的元素视图,然后采取截图。然后,我们需要裁剪基于元素的位置的图像。

  3. 此外,Point类也不是很可靠的给元素的确切位置。

  4. 其次,如果我们看到下面的值

    ImageIO.read(screenshot).getHeight() // ~ 943 => Total height
    element.getSize().getHeight() // ~ 511 => Element height 
    point.getY() // ~ 743 => start top side y coordinate of element
    
  5. 所以,我相信,当矩形从X绘制,Y与743高度它去原截图坐标出来。

因此,我们需要同时通过坐标进行一些调整。

@Test
public void subImageTest() throws InterruptedException, IOException {
    driver.get("http://learn-selenium-easy.blogspot.com/");
    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,600)");
   File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    WebElement element=driver.findElement(By.xpath("//*[@id='PopularPosts1']"));
    System.out.println(element.getSize());

  // Take full screen screenshot
    BufferedImage  fullImg = ImageIO.read(screenshot);
    ImageIO.read(screenshot).getHeight()
    System.out.println(fullImg.getHeight()); 
    System.out.println(fullImg.getWidth());

    Point point = element.getLocation();
    int elementWidth = element.getSize().getWidth(); 
    int elementHeight = element.getSize().getHeight();

    // Now no exception here
    BufferedImage elementScreenshot= fullImg.getSubimage(220, 170,elementWidth+150,elementHeight+100);

    // crop the image to required
    ImageIO.write(elementScreenshot, "png", screenshot);
    FileUtils.copyFile(screenshot, new File("C:\\Users\\AppData\\Local\\Temp\\mostread_screenshot.png"));//path to save screen shot

}

这里元素的韵母子图像程序执行后, 在这里输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

Java的:是'sun.awt.image`包过时?

File中的java.awt.Image

什么是java.awt.image.BufferedImage对于屏幕外渲染最有用的类型?

可以使用Java.awt.Image android应用程序

如何最好地序列化java.awt.Image?

将位图转换为java.awt.image.BufferedImage

使用AWT和Java组件进行绘图

如何减少矩形的高度?(Java,AWT)

为什么在OSX上复制和粘贴图像现在在Java 8中返回sun.awt.image.MultiResolutionImage

mysql blob图片到awt.image,然后awt.image到blob,不改变大小

Java-使用AWT更改JFrame的标题

使用桌面打开文件(java.awt)

使用JButton停止java.awt.Robot

从单独的方法中使用 java awt drawRect?

JRE选项java.awt.headless和java.awt.headlesslib有什么区别?

在调用java.awt.image.RenderedImage类的getWidth()方法时获取java.security.AccessControlException

使用java.awt.image.BufferedImage中创建BIFF8 BITMAP记录花费时间 - 有没有更好的方法吗?

Java图形AWT

Java AWT矩形交集

Java AWT范围

Java AWT 字体乱码

您如何确定图像(java.awt.Image)是否为黑白且没有颜色?

java.awt.image.BufferedImage.getRBG不返回期望值

如何在Android Studio中导入java.awt.image.BufferedImage

缩放图像会在java.awt.image.ReplicateScaleFilter。<init>(Unknown Source)处产生错误

CodenameOne中的java.awt.image.BufferedImage的替代方案是什么

Java Swing窗口大小与AWT画布结果不匹配

Java使用PNG图像作为字体(Java.awt.graphics)