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:

package TakingScreenshot;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class CaptureScreenshotUsingRobot {
	public static String CaptureScreenShotWithTestStepNameUsingRobotClass(String testStepsName)
			// Creating Robot class object
			Robot robotClassObject = new Robot();
			// Get screen size
			Rectangle screenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
			// Capturing screenshot by providing size
            BufferedImage tmp = robotClassObject.createScreenCapture(screenSize); 
            // Defining destination file path
            String path=System.getProperty("user.dir")+"/ScreenCapturesPNG/"+testStepsName+System.currentTimeMillis()+".png";
            // To copy temp image in to permanent file
            ImageIO.write(tmp, "png",new File(path)); 
            return path;
		}catch(Exception e)
			System.out.println("Some exception occured." + e.getMessage());
			return "";

	public static void main(String[] args) throws InterruptedException {
		ChromeDriver driver= new ChromeDriver();
		CaptureScreenshotUsingRobot.CaptureScreenShotWithTestStepNameUsingRobotClass("URL Loading");




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.




9 thoughts on “Capturing Screenshot Using Robot Class In Selenium

Leave a Reply

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