Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Taking Screenshot Using TakesScreenshot Interface In Selenium

Posted on 03/21/2025 By admin

Hello Folks,

In this post we will learn how to take screenshots in Selenium webdriver using selenium provide interface TakesScreenshot .

Why we need to take screenshot?

When we perform manual testing , for each step or important steps, we take screenshot and attach in test result report. Screenshots are proof of test case status. It gives information if test case has passed successfully or reasons if test case has failed.

Taking screenshot of browser by using “TakesScreenshot” interface provided by Selenium:

  • Selenium provides an interface named “TakesScreenshot” which helps automaton engineer to take screenshot during execution of automation script.
  • “WebElement” interface extends “TakesScreenshot” and all browser driver classes like “ChromeDriver”, “FirefoxDriver”, “InternetExplorerDriver”, “EdgeDriver”  etc implements “TakesScreenshot”.
  •  TakesScreenshot interface has a method named “getScreenshotAs” which can capture a screenshot and store it in different ways in specified locations.
  • This method can capture screenshot as below based on implementation provided by browser driver class:
    • Entire page
    • Current window
    • Visible portion of the current frame
    • The screenshot of the entire display containing the browser
  • I will suggest you to must go throw Hierarchy of Selenium Classes and Interface before proceeding further in this post.
  • To use “TakesScreenshot” interface, either we need to do up-casting or down casting depending upon reference of your browser driver class.
  • If you write your browser driver class as below:

ChromeDriver driver= new ChromeDriver();

You need to up-cast your driver to TakesScreenshot as below:

TakesScreenshot ts= driver;

  • If you write your browser driver class as below:

WebDriver  driver= new ChromeDriver();

You need to down cast your driver to TakesScreenshot as shown below:

TakesScreenshot ts= (TakesScreenshot)driver;

Java Code:

Output:

Explanation of above code:

  • Since I have upcasted my ChromeDriver object to WebDriver, to capture screenshot I need to downcast my  ChromeDriver object to TakesScreenshot  interface.
  • “OutputType” is an interface which provides you options to take screenshot in different type such as FILE, BASE64, BYTES and class. FILE is mostly used.
  • When webdriver tales screenshot, it is stored in temp folder which is cleaned up as soon as execution is over. So we can not find it after execution. This is the reason we need to copy screenshot from temp folder to permanent folder.
  • Above code will create a folder within project folder named “ScreenCapturesPNG”. Screenshot will be named by concatenating user provided name and UNIX time stamp.
  • I created a reusable method to capture screenshot so that we can use it wherever we need and also you can pass customized name to screenshot.
  • I am also returning path of screenshot, which we can use to attach screenshot in to a html report, extent report etc.

Limitation of using TakesScreenshot  interface for capturing screenshot:

  • Above code will not able to capture screenshot when an alert is present. It will give you an exception stating “org.openqa.selenium.UnhandledAlertException”.
  • This is a known defect whose details you can find here.
  • I will share another way of taking screenshot which will work always.

I hope you must have learnt something new from this post.

If you have any doubt, feel free to ask here.

If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium

Uncategorized

Post navigation

Previous Post: webdriver quiz
Next Post: difference

Related Posts

MultiplePkgTestng – Make Selenium Easy Uncategorized
read excel data using apache poi Uncategorized
API Testing Tutorial Part 11 – Difference Between SOAP and Rest Web Services or API Uncategorized
image – Make Selenium Easy Uncategorized
Make Selenium Easy | Make Selenium Easy – Selenium Tutorials : End To End Uncategorized
Create a Map From Excel Data in Java – Selenium – API Automation Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark