zoukankan      html  css  js  c++  java
  • Selenium: java.awt.image.RasterFormatException: (y + height) is outside of Raster when using element size and location

    1. The element image you are cropping is not present in the screenshot taken by the code. If you put debug and print full screen shot path and manually view it then you can see desired element to be cropped from image is not present in it.

    2. So first we need to scroll page to bring desired element into view and then take the screenshot. Then we need to crop the image based on element's location.

    3. Also, Point class is not very reliable to give element's exact location.

    @Test
    public void subImageTest() throws InterruptedException, IOException {
        driver.get("http://www.reyo.cn/");
        ((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:\\reyo\\mostread_screenshot.png"));//path to save screen shot
    
    }
  • 相关阅读:
    DateGridView 分页显示
    DataGridView 隔行显示不同的颜色
    DataGridview 绘制行序号
    DataGrridView 当前行显示不同颜色
    右键删除行
    【bzoj2763】[JLOI2011]飞行路线 分层图最短路
    【bzoj1143】[CTSC2008]祭祀river Floyd+网络流最小割
    【bzoj1029】[JSOI2007]建筑抢修 贪心+堆
    【bzoj1054】[HAOI2008]移动玩具 Bfs
    【bzoj1911】[Apio2010]特别行动队 斜率优化dp
  • 原文地址:https://www.cnblogs.com/interdrp/p/15523516.html
Copyright © 2011-2022 走看看