我有从本教程中获取的代码。它会转到 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);。我不确定为什么会这样,因为图像的点和大小是从元素本身获取的。
江户川乱折腾
一只名叫tom的猫
相关分类