Make Selenium Easy

Capturing Screenshot Using Robot Class In Selenium

Hello Folks,

In last post we have seen Taking Screenshot Using TakesScreenshot Interface In Selenium. I have explained about its drawbacks as well.

Drawbacks of using TakesScreenshot interface for taking screenshot in selenium:

  • It will not take screen shot when an alert is open.  It will give you an exception stating “org.openqa.selenium.UnhandledAlertException”.
  • It takes screenshot of visible area of browser. It will not capture address bar and tab opened.
  • If your requirement is to take screenshot with URL, it will not be possible through above interface.

Capturing screenshot in Selenium using Robot class:

  • Java provides a class named “Robot” which is present in java.awt package.
  • Using Robot class, we can control over the mouse and keyboard devices.
  • Robot class generates native system input events for the purposes of test automation of applications where control of the mouse and keyboard is needed.
  • Robot class will capture screenshot successfully if any alert is open.

Steps to capture screenshot using Robot class:

  • Create an object of Robot class.
  • Get the screen size as a Rectangle.
  • Use createScreenCapture of Robot class and capture screenshot. It will be temp image.
  • Define destination path for screenshot.
  • Write temp file in to permanent file.

Java code:


You can see it takes screenshot of whole desktop. It gives extra information about screenshot such as URL, Time and date.

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

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




Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

9 thoughts on “Capturing Screenshot Using Robot Class In Selenium

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.