Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » Capture Screenshot Selenium WebDriver

Capture Screenshot Selenium WebDriver

Most of the time we think to Capture Screenshot in Selenium WebDriver when some kind of error or exception surfaces while practicing testing, to resolve the same WebDriver has provided us one interface TakesScreenshot for capturing the screenshot of web application and This interface provides one method names as getScreenshotAs() to capture screenshot in the same instance of driver. This getScreenshotAs() method takes argument of type OutputType.File or OutputType.BASE64 or Output.BYTES. So that it could return captured screenshot in File type, or Base 64 string type or in raw bytes.
So this would look like this

For File type
File scrnshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

For Base64 string above code would be like


We have taken the screenshot with the help of getScreenshotsAs() method and  and now its time to copy this file somewhere in our file system or in our desktop. So for this purpose we further use copyFile() method of the FileUtils class from the org.apache.commons.io.FileUtils class.

Placing file in file system by using this line

FileUtils.copyFile(scrFile, new File(“e:\main_page.png”));

As I have told you that copyFile is a method of Class FileUtils and to call this method we need to write class.method() and in above code copyFile is taking argument from getScreenShotsAs() and new location where we want to save this captured Screenshot with name and with extension.

Now we would use write small running code that would open Google page and it would take a snap shot and that snap shot would be saved as google.png in my e: driver. One thing we need to remember whenever we work with File system or java.io then chances of exception is more so we would use try and catch in our code.

import java.io.File;
import org.apache.commons.io.FileUtils;
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.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Chrome {
WebDriver driver;
public void launchChrome()
System.setProperty("webdriver.chrome.driver", "E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe");
driver = new FirefoxDriver();
public void googleScreenshot()
try {
File scrnsht =
FileUtils.copyFile(scrnsht, new
} catch (Exception e) {
public void kill()

Original Resource available on this place

23 thoughts on “Capture Screenshot Selenium WebDriver”

  1. HTTP://Gita92.datingish.Com/

    I’m so happy to read this. This is the type of manual that needs to be given and not the

    accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.

  2. It’s in reality a nice and helpful piece of

    info. I am satisfied that you simply shared this helpful

    info with us. Please stay us up to date like this. Thank you for

  3. Pingback: Selenium Best Links | Learn Testing

  4. Thanks a lot for your valuable information to on capture screenshot using selenium webdriver.

    It could be useful if you you provide more in depth information on Edit box,Check/Uncheck,Drop down,scrollpage,radion button,Link text implicit wait condition, etc.,,

      1. java.lang.UnsupportedOperationException: captureEntirePageScreenshot
        at org.openqa.selenium.WebDriverCommandProcessor.execute(WebDriverCommandProcessor.java:137)
        at org.openqa.selenium.WebDriverCommandProcessor.doCommand(WebDriverCommandProcessor.java:70)
        at com.thoughtworks.selenium.DefaultSelenium.captureEntirePageScreenshot(DefaultSelenium.java:723)
        at testNG.AccountMisMatch.generateBulkReturn(AccountMisMatch.java:59)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

  5. Pingback: Handling JavaScript Alert in WebDriver | Abode QA

  6. i am using same code which working well on normal pages
    buton a page with a video running like youtube the screenshot of video is coming as black

    can u check once and suggest any solution

  7. Hi

    I am trying to take screen shot using following code but its not working
    File scht=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scht, newFile(“c:\\google_page.png”));

    newFile is showing error saying method newFile(String) is undefined. Can you kindly help me to solve this issue
    Thank you

  8. Hi All,

    I have Flex application in my project . Can anyone suggest me, What are the additional setup needs to be done.

  9. Thank you. I’ve been looking for this for a while since in the IDE is pretty simple but the I couldn’t make it work using the backed webdriver after exporting.

  10. Is there any way to get screen shot of the entire page using selenium and java.I tried many API’s present in web but for chrome and IE they are not working.Please suggest.

  11. Thanks for your Post! Really it will be helpful for both beginners and experienced professional. Your information is very clear.
    Along with your post, I put my input here.
    The following example is in Java:

    WebDriver driver = new FirefoxDriver();
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    // Now you can do whatever you need to do with it, for example copy somewhere
    FileUtils.copyFile(scrFile, new File(“c:\\tmp\\screenshot.png”));

    If your audience is also interested in Selenium Testing, they can take a look here: Selenium Training

  12. This article is really very helpful but if the screenshot is deleted than i won’t able to see screenshot in extent report or if i send this report to someone else they won’t see that snapshot in report. Can anyone suggest some way to store snapshot on web so that it can be accessed anytime or some other better way.

    Arpit Jain

  13. Hi,

    I am getting error at this point:
    File screenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    Exception taking screenshot is null.

    The code is:
    public static String getScreenshot(WebDriver getDriver, String ScreenshotName)
    String dateName = new SimpleDateFormat(“yyyyMMddhhmmss”).format(new Date());
    // TakesScreenshot ts = (TakesScreenshot)driver;
    File source = ((TakesScreenshot)getDriver).getScreenshotAs(OutputType.FILE);
    String dest=System.getProperty(“user.dir”) + “/FailedTestsScreenshots/”+ScreenshotName+dateName+”.png”;
    File snapshotDest =new File(dest);
    FileUtils.copyFile(source, snapshotDest);

    System.out.println(“Screenshot Taken at “+System.currentTimeMillis());

    return dest;


    catch (Exception e)
    System.out.println(“Exception while taking screenshot “+e);
    return e.getMessage();

    Tried calling this method in another class:

    public void teardown(ITestResult result) throws Exception
    if (result.getStatus()==ITestResult.FAILURE)
    test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + “Test Case failed”, ExtentColor.RED));
    String screenshotPath = screenshot.getScreenshot(driver, result.getName());
    test.log(Status.FAIL, screenshotPath);

    else if(result.getStatus()==ITestResult.SUCCESS)
    test.log(Status.PASS, MarkupHelper.createLabel(result.getName() + “Test Case passed”, ExtentColor.GREEN));
    String screenshotPath = screenshot.getScreenshot(driver, result.getName());
    test.log(Status.PASS, screenshotPath);

    test.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + “Test Case skipped”, ExtentColor.YELLOW));

    String screenshotPath = screenshot.getScreenshot(AllDrivers.getDriver(), result.getName());


    I tried figuring out a solution,but i was unable to.Please help me with a solution.

    Anideep .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: