How to take the screenshot of specific element in selenium webdriver
Like our facebook page www.facebook.com/ankprotraining
Capturing Screenshot in selenium webdriver :
Selenium WebDriver provides the capability to capture screenshots during a test execution.
It is always good to capture the screenshot of the test results if you are running the tests overnight.
The capability to take a screenshot with selenium is extracted from the "TakesScreenshot" interface that allowing us to take a screenshot of the current page displayed in the driver instance.
Possible Interview Questions on selenium webdriver screen capture for a specific element:
How to take the screenshot of specific element
How To Take Screenshot Of Specific Element code :
@Test
public void CaptureScreenOfSpecificElement() throws IOException
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ankpro.com");
WebElement ele = driver.findElement(By.className("jumbotron"));
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
FileUtils.copyFile(screenshot, new File("test.jpeg"));
driver.quit();
}