Capture Screenshot of Web Page In Selenium WebDriver

Most of the time we think to Capture Screenshot in 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 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 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 then chances of exception is more so we would use try and catch in our code.
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("", "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 

16 thoughts on “Capture Screenshot of Web Page In Selenium WebDriver

  1. 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

  2. Pingback: Selenium Best Links | Learn Testing

  3. 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.,,

      • java.lang.UnsupportedOperationException: captureEntirePageScreenshot
        at org.openqa.selenium.WebDriverCommandProcessor.execute(
        at org.openqa.selenium.WebDriverCommandProcessor.doCommand(
        at com.thoughtworks.selenium.DefaultSelenium.captureEntirePageScreenshot(
        at testNG.AccountMisMatch.generateBulkReturn(
        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(
        at org.testng.internal.Invoker.invokeMethod(
        at org.testng.internal.Invoker.invokeTestMethod(
        at org.testng.internal.Invoker.invokeTestMethods(
        at org.testng.internal.TestMethodWorker.invokeTestMethods(
        at org.testng.TestRunner.privateRun(
        at org.testng.SuiteRunner.runTest(
        at org.testng.SuiteRunner.runSequentially(
        at org.testng.SuiteRunner.privateRun(
        at org.testng.SuiteRunnerWorker.runSuite(
        at org.testng.TestNG.runSuitesSequentially(
        at org.testng.TestNG.runSuitesLocally(
        at org.testng.remote.RemoteTestNG.initAndRun(
        at org.testng.remote.RemoteTestNG.main(

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

  5. 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

  6. 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

  7. Hi All,

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>