使用selenium webdriver截取网页的一部分

时间:2017-02-16 08:07:27

标签: java selenium

是否可以使用selenium web驱动程序捕获web ui的特定部分(例如使用图像的协调?不是整个页面!)我用java编写脚本。感谢

2 个答案:

答案 0 :(得分:0)

我认为这部分代码会对您有所帮助。

driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo"));

// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);

// Get the location of element on the page
Point point = ele.getLocation();

// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();

// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
    eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);

// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);

答案 1 :(得分:0)

只想分享我是如何使用Multiscreenshot jar解决这个问题的。感谢。

import multiScreenShot.MultiScreenShot; 

WebElement object1 = driver.findElement(By.Id("MyObject"));
object1.click();
MultiScreenShot multiScreens = new MultiScreenShot("C:\\", "test"); // Path where you save the image
multiScreens.elementScreenShot(driver, object1);
WebElement object2 = driver.findElement(By.Id("YourObject"));
object2.click();
multiScreens.elementScreenShot(driver, object2);