我们可以通过裁剪整个页面截图来获得元素截图,如下所示:
driver.get("http://www.google.com");WebElement ele = driver.findElement(By.id("hplogo"));// Get entire page screenshotFile screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);BufferedImage fullImg = ImageIO.read(screenshot);// Get the location of element on the pagePoint point = ele.getLocation();// Get width and height of the elementint eleWidth = ele.getSize().getWidth();int eleHeight = ele.getSize().getHeight();// Crop the entire page screenshot to get only element screenshotBufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);ImageIO.write(eleScreenshot, "png", screenshot);// Copy the element screenshot to diskFile screenshotLocation = new File("C:\images\GoogleLogo_screenshot.png");FileUtils.copyFile(screenshot, screenshotLocation);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)