How to Take a Screenshot of Specified Content Using Selenium Webdriver


In general, when we take a Screenshot in selenium we get the output screenshot of the entire page.In some cases it is not even helpful.So in order to suppress this, we can take a screenshot for some specific content which is useful for us to report.This Post helps you to capture screenshot of Specified Content.

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import jxl.Sheet;
import jxl.Workbook;

public class screenshot {

@Test

public  void cropScreenShot() throws Exception
{

FirefoxDriver fd=new FirefoxDriver();
fd.get(“http://www.yahoomail.com”);
fd.findElement(By.name(“login”)).sendKeys(“Dollar”);
fd.findElementByName(“passwd”).sendKeys(“Password”);
fd.findElementByName(“.save”).click();
Thread.sleep(2000);
WebElement we=fd.findElement(By.xpath(“.//*[@id=’fsLogin’]/div/strong”));
File v=fd.getScreenshotAs(OutputType.FILE);
BufferedImage bi=ImageIO.read(v);
org.openqa.selenium.Point p=we.getLocation();
int n=we.getSize().getWidth();
int m=we.getSize().getHeight();
BufferedImage d=bi.getSubimage(p.getX(), p.getY(), n, m);
ImageIO.write(d, “png”, v);

FileUtils.copyFile(v, new File (“D:\\yahooError.jpg”));
}
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s