java+selenium 截取某一元素的图片的方法

来源:互联网 发布:淘宝店怎么退出保证金 编辑:程序博客网 时间:2024/05/16 08:38

参数中的path为文件的存放地址

/**     * This method for screen shot element     *      * @param driver     * @param element     * @param path     * @throws InterruptedException     */    public static void screenShotForElement(WebDriver driver,            WebElement element, String path) throws InterruptedException {        File scrFile = ((TakesScreenshot) driver)                .getScreenshotAs(OutputType.FILE);        try {            Point p = element.getLocation();            int width = element.getSize().getWidth();            int height = element.getSize().getHeight();            Rectangle rect = new Rectangle(width, height);            BufferedImage img = ImageIO.read(scrFile);            BufferedImage dest = img.getSubimage(p.getX(), p.getY(),                    rect.width, rect.height);            ImageIO.write(dest, "png", scrFile);            Thread.sleep(1000);            FileUtils.copyFile(scrFile, new File(path));        } catch (IOException e) {            e.printStackTrace();        }    }
0 0
原创粉丝点击