How to Take A Screenshot in Selenium WebDriver? | A step by step Guide
How to Take A Screenshot in Selenium WebDriver

How to Take A Screenshot in Selenium WebDriver? | A step by step Guide

Last updated on 13th Jun 2020, Blog, General

About author

Pradeep (Sr Testing Manager )

He is a TOP-Rated Domain Expert with 6+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 3 Years & Share's this Informative Articles For Freshers

(5.0) | 18793 Ratings 1290

Take Screenshot in Selenium WebDriver: When I am working as manual testers, that time when we are testing an application, and if we found any bug then we are taking the screenshot of that and send that to a developer so that they can go through the bug and try to fix. As a part of testing, the screenshot is played a major role because without that its too difficult to influence the developer.

Similarly, when we are automate something in selenium webdriver, we may also come across bugs, and in that case, we need to capture the screenshots. So for taking screenshot selenium provides this feature. With this, we can be debugging the errors and also get to know which step the test goes to fail.

How To Take Screenshot?

Selenium webdriver has the built-in functionality to take the screenshot of the web page and its quite easy well. Because selenium webdriver has an interface called TakesScreenshot, which can capture the screenshot during the execution of the selenium script.

    Subscribe For Free Demo


    In the TakesScreenshot interface, we have a method called getScreenshotAs(), which can capture the screenshot and store that in a specified location by us. By using this method, we can take the screenshot of various browsers like Chrome, Firefox, IE, Opera, etc. using their respective web-drivers.

    The WebDriver interface extends the TakesScreenshot, and all browser drivers like ChromeDriver, FirefoxDriver, InternetExplorerDriver, EdgeDriver, OperaDriver these all are implementing the TakesScreenshot interface, that’s why in all browser we can take the screenshot.

    Note: The Selenium webdriver extending the TakesScreenshot interface and by using the getScreenshotAs() method, this method only capable of capturing the visible area of the web page and not the full page.

    • import;
    • import;
    • import java.util.concurrent.TimeUnit;
    • import;
    • import org.openqa.selenium.OutputType;
    • import org.openqa.selenium.TakesScreenshot;
    • import org.openqa.selenium.WebDriver;
    • import;
    • public class SeleniumScreenshotTest
    • {
    •   public static void main(String[] args) throws IOException
    •   {
    •      System.setProperty(“” , “C:\\drivers\\chromedriver.exe”);
    •      WebDriver driver = new ChromeDriver();
    •      driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    •      driver.manage().window().maximize();
    •      driver.get(“”);
    •     // down casting WebDriver to use getScreenshotAs method.
    •      TakesScreenshot ts= (TakesScreenshot)driver;
    •     // capturing screen shot as output type file
    •      File screenshotSRC= ts.getScreenshotAs(OutputType.FILE);
    •     // Defining path and extension of image
    •      String path=System.getProperty(“user.dir”)+”/ScreenCapturesPNG/”+testStepsName+System.currentTimeMillis()+”.png”;
    •     // copying file from temp folder to desired location
    •      File screenshotDest= new File(path);
    •      FileUtils.copyFile(screenshotSRC, screenshotDest);
    •      driver.quit();
    •   }
    • }

    Explanation Of The Above Code

    • WebDriver driver = new ChromeDriver();

    In the above statement we have upcasted the chromedriver object to webdriver, but for capture the screenshot we need to downcast the chromedriver object driver to TakesScreenshot interface.

    • TakesScreenshot ts= (TakesScreenshot)driver;
    • File screenshotSRC= ts.getScreenshotAs(OutputType.FILE);

    In the above statement, the OutputType is an interface which can provide options to take the screenshot in different types such as FILE, BASE64, BYTES, and class. But out of those types, the file type is mostly used.

    Course Curriculum

    Get Hands-on Experience from Selenium WebDriver Certification Course

    Weekday / Weekend BatchesSee Batch Details
    • path=System.getProperty(“user.dir”)+”/ScreenCapturesPNG/”+testStepsName+System.currentTimeMillis()+”.png”;

    When webdriver takes the screenshot, it stores in the temp folder. Once the execution is over the file also got deleted. That’s why we need to copy the screenshot in a permanent folder, so for that in the above statement we are mentioned the permanent folder which is ScreenCapturesPNG with the file name.

    • File screenshotDest= new File(path);

    In this statement, we are creating a file where we mentioned where it should be stored with a specific name.

    Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    How Do You Take Screenshot of The Entire Page?

    For taking the full page screenshot, we can take the help of the third-party tools like AShot to take the whole page screenshot or a specific element screenshot. Now we are going to discuss the AShot tools, and these tools have a rich feature like:

    • Take a specific area of a page
    • Do scroll and take a screenshot
    • In case you have an infinite scroll page, you can set timeouts
    • Ignore are during the screenshot
    • Change orientation or change resolution

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free