How to Capture Screenshot in Selenium Webdriver-3 different ways

Test Automation

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over.

In this blog post we will see how to capture screenshot, there are different ways to capture screenshots.

Which includes-

1- Capture full Page
2- Capturng full page(enabling scrolldown)-Using Shutterbug
3- Capturing screenshot of particular web object-

1- Capturing full page(without doing scrolldown)-

Below method can help in capturing screenshot of full page(excluding scroll bar)

public void TestScenario() throws Exception
WebDriver driver ;

String exePath = "E:\\chromedriver_win32\\chromedriver.exe";

System.setProperty("", exePath);

//Chrome driver declaration
driver = new ChromeDriver();

//Launch url

//Call take screenshot function
this.ScreenCapture(driver, "c://test.png") ; }

Above code is calling ScreenCapture, where 2 arguments are required- driver and file path, Have a look below

public static ScreenCapture(WebDriver driver,String filePath) throws Exception
//Referring web driver object to TakeScreenshot, which is being sent as an argument
TakesScreenshot scrShot =((TakesScreenshot)driver);

//Call getScreenshotAs method to create an image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

//Move image to new destination
File DestFile=new File(filePath);

//Copy file at destination location
FileUtils.copyFile(SrcFile, DestFile);


2- Capturng full page(enabling scrolldown)-Using Shutterbug-

Shutterbug is a java jar file using which fullpage screenshot can be taken, Even when there is vertical scroolbar is available, Below code can be used afterconfiguring shutterbug jar file.

Shutterbug download

Call below code whenever fullpage screencapture is required, Only driver and screenshot name needs to be passed as an argument.

public void FullPageScreenCapture(driver, String screenshotname)
String timeStamp = new SimpleDateFormat(“yy_MM_dd_HH_mm_ss”).format(new Date());

String screenshot_name = null ;

screenshot_name = screenshotname+”_”+timeStamp;


3- Capturing screenshot of particular web object- Button, Lable, Radiobutton and checkbox’s etc

It may be required to captured only specific webobject in a webpage, sam ecan be done using below piece of code. In Which that particular webobject is passed while calling below method along with driver object.

public void WebObjectCapture(driver, )
WebElement ele = driver.findElement("q"));

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

// 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:\\Screenshot\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);



In User Interface/Browser automation testing it is recomended to capture all test logs, Pass, Fail therefore significance of screenshots is alot in test automation.

A neatly crafted automation framework does have all required loogers configured which comprises of text logs along with screenshots etc.

Calling above screenshot method needs to be done in such a way so that minimal code is required to write, Best way is to call them in Listners class. While exteding ITestListners to a class there are couple of methods where above method can be calleds.

Methods in ITestListners are-

1- onFinish
2- onStart
3- onTestFailureButWithSuccessPercentage
4- onTestFailure
5- onTestSkipped
6- onTestStart
7- onTestSuccess

It is recomended to use screenshot capturing methods at onTestFailure only.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Selenium Basic
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
What is Headless browser automation

Hello Readers, In this post we will read about headless browser automation and how to use or invoke different headless browser using selenium. First of all lets understand What is a headless browser? A headless browser is a web-browser without a GUI-Graphical User Interface. The major difference with browser having …