如何使用Selenium Webdriver捕获特定元素而不是整个页面的屏幕截图?

如何使用Selenium Webdriver捕获特定元素而不是整个页面的屏幕截图?,第1张

如何使用Selenium Webdriver捕获特定元素而不是整个页面的屏幕截图

我们可以通过裁剪整个页面截图来获得元素截图,如下所示:

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);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5048412.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存