public static BufferedImage createElementImage(WebElement webElement,
WebDriver webDriver,int x,int y) throws IOException {
ByteArrayInputStream by = null;
BufferedImage croppedImage = null;
try {
long s = System.currentTimeMillis();
// 获得webElement的位置和大小。
Point location = webElement.getLocation();
Dimension size = webElement.getSize();
by = new ByteArrayInputStream(takeScreenshot(webDriver));
// 创建全屏截图。
BufferedImage originalImage = ImageIO.read(by);
// 截取webElement所在位置的子图。
croppedImage = originalImage.getSubimage(location.getX()+x,
location.getY()+y, size.getWidth(), size.getHeight());
long e = System.currentTimeMillis();
logger.error("获取webelement 验证码 流的时间为:" + (e - s));
} finally {
if(by!=null){
by.close();
}
}
return croppedImage;
}