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

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

Capturing Screenshot Using Robot Class In Selenium

by Amod Mahajan time to read: 2 min
%d bloggers like this: