Robot Class in Selenium Webdriver

Robot Class in Selenium Webdriver

Last updated on 21st Sep 2020, Artciles, Blog

About author

Aravind (Sr Testing Manager )

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

(5.0) | 15737 Ratings 637
  • Using Robot Class we can handle keyboard and mouse events in Selenium.
  • Robot Class can help in upload / download files using selenium web driver.
  • Methods in Robot Class can be effectively used to do the interaction with popups in Web Applications.
  • Robot Class can easily be integrated with current automation framework (keyword, data-driven or hybrid).

Why Robot Class?

  • In certain Selenium Automation Tests, there is a need to control keyboard or mouse to interact with OS windows like Download pop-up, Alerts, Print Pop-ups, etc. or native Operating System applications like Notepad, Skype, Calculator, etc.
  • Selenium Webdriver cannot handle these OS pop-ups/applications.
Subscribe For Free Demo

Error: Contact form not found.

Benefits of Robot Class

  • Robot Class can simulate Keyboard and Mouse Event
  • Robot Class can help in upload/download of files when using selenium web driver
  • Robot Class can easily be integrated with current automation framework (keyword, data-driven or hybrid)

Robot Class Handle Below Options

  • Press Escape key from keyboard using Different Robot class Methods.
  • Handle Mouse Buttons Using Robot class.
  • Scroll Mouse Using Robot class.
  • Screen Capture Using Robot class.


Using below packages we handle Robot Class Methods.

  • import java.awt.Robot;
  • import java.awt.event.KeyEvent;
  • import java.awt.AWTException;

 Methods under Robot Class are:

  • .keyPress();
  • .mousePress();
  • .mouseMove();
  • .keyRelease();
  • .mouseRelease();

Press Escape key on keyboard using Different Robot class Methods:


Using keyPress() Method we Press any key from KeyBoard.

  • robot.keyPress(KeyEvent.VK_ESC);// This will press the Escape key on the keyboard


Using keyRelease() Method we Release the Pressed any key from KeyBoard.

  • robot.keyRelease(KeyEvent.VK_CAPS_LOCK);// This will release the CAPS_LOCK key


Using mousePress() Method we press the Left mouse button from Mouse.

  • robot.mousePress(InputEvent.BUTTON1_MASK);// This will press the Left mouse button.

 4. .mouseRelease()

Using mouseRelease() Method we Release pressed Left mouse button from Mouse.

  • robot.mouseRelease(InputEvent.BUTTON1_MASK);// This will release the Left mouse button.

  5. .mouseMove()

Using mouseMove() Method used to Move cursor pointer to X and Y coordinates.

  • robot.mouseMove(coordinates.getX(), coordinates.getY());//This will move the mouse pointer to X and Y coordinates.

Use Of Robot Class In Java

When and where we can use Robot Class in Java to enhance the functionality effectively in an automation framework?

As we all know that Selenium can be used to automate web applications. These web applications in Selenium use the underlying web object (locators) of the web app to determine the position on the web page and operate accordingly. Now in certain cases, we see that we need to handle windows authentication pop-up or any other windows pop-up while automating on the Windows operating system.

Selenium cannot automate Windows actions but we can use Robot Class in Java to accomplish the task.

Java Robot Class Examples

Example 1: Using Robot Class to Automate some Windows functionalities

Sometimes any small part/module of the entire web application may be designed in flash. We can use the Robot class to handle that part. Since web objects remain hidden for the part designed in flash.

Another important feature that can be automated using Robot Class in Java is Rich Text Editors, which sometimes remain embedded in web applications. There are several operational functions in RTF editors which is quite easy to automate using this class.

Example 2:Using Robot Class for automating RTF fields

Note: There are other open-source tools available in the market like Auto IT (specially designed to automate windows GUI), Sikuli (image processing tool), etc. to accomplish similar tasks.

This is true but Robot Class in Java comes with JDK. Thus, we do not require any other tool to install, this is a straight advantage if one is using a Java-based automation framework.

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


But methods mentioned above to control Keyboard and Mouse have some limitations also. Consider some of those following limitations while writing automation scripts: 

  • Most of the methods like mouseMove are dependent on screen resolution, so, the method may perform differently on different screens.
  • This class acts only on the window in focus, so the behavior may differ when multiple windows open.
  • Switching between different frames or windows is difficult with Robot methods.


To conclude, the purpose of this tutorial is to get an introduction to the Robot class. We will be covering Keyboard Events and Mouse Events methods in more detail in our next articles.

Are you looking training with Right Jobs?

Contact Us

Popular Courses