slenium截屏
创建全屏截屏:
public static byte[] takeScreenshot(WebDriver driver) throws IOException {
WebDriver augmentedDriver = new Augmenter().augment(driver);
return ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.BYTES);
}
创建固定位置截屏:
public static BufferedImage createElementImage(WebDriver driver, WebElement webElement) throws IOException {
// 获得webElement的位置和大小。
Point location = webElement.getLocation();
Dimension size = webElement.getSize();
// 创建全屏截图。
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(takeScreenshot(driver)));
// 截取webElement所在位置的子图。
BufferedImage croppedImage = originalImage.getSubimage(location.getX(), location.getY(), size.getWidth(),size.getHeight());
return croppedImage;
}