Taking Screenshot using Webdriver

Its very important to take screenshot when we execute a test script. When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.

It helps us to debug and identify the problem by seeing the screen shot.

In selenium webdriver, we can take the screen shot using the below command.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

NOTE: - If you are using latest version of selenium, please check this updated article Take Screenshot using FileHandler class

Check the framework example of Taking ScreenShot for ONLY Failed Tests using TestNG
The below example explains how to take the screen shot when the test fails.

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
    public class takeScreenShotExample{
        public WebDriver driver;
      public void openBrowser() throws Exception {
    	  driver = new FirefoxDriver();
                //the below statement will throw an exception as the element is not found, Catch block will get executed and takes the screenshot.
                   //if we remove the below comment, it will not return exception and screen shot method will not get executed.
    	  catch (Exception e){
    		  System.out.println("I'm in exception");
//calls the method to take the screenshot.
      public void getscreenshot() throws Exception 
              File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
           //The below method will save the screen shot in d drive with name "screenshot.png"
              FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
Selenium Tutorials: 


Hi, could u pls suggest how to take screenshot of an alert.

how to take full page screen shot using google chrome

Add "full page screen capture chrome" Add-on in your chrome browser

this program will always override the newly taken screenshot.
What if we want to retain the old screenshots too

You can just name the screenshot with current date and time, which will create new image every time, But you need to make sure, delete all the file if they are no more useful.

Give new name in
FileUtils.copyFile(filesrc, new File("E:\\Screenshots\\Changenamehere.png"));

Change the name of the screenshot.


Is there any way in Selenium ,we can take the Screenshot and not save it on any local machine (any path) ?

Or , we can delete the Screenshot taken by Selenium ( not manually) ?


Hi Angad,
If I undestood correctly, you dont want to save the screenshot file right ?if so then you should not use FileUtils.copy method here.. thats it. The screeshot taken by this program will only be saved if you use the FileUtils.copy, and the screenshot file will anyway vanish once your program gets out of the JVM.

FileUtils.copyFile(scrFile, new File("D:\\screenshot.png")); Here i got an error saying that copyFile is undefined by FileUtils.
Can you please help me out.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.