How to use Robot Class in Selenium using Java? Step-by-step guided tutorial!


When you want to test an application using selenium,  You need a few functions that might save up time during execution.  One such method that helps in controlling certain functions of the system is by using a “robot class.” 

In this session, we’ll understand the importance of a robot class in selenium, while testing an application so let’s begin this Article by taking a look at the agenda. So we’ll first start by understanding what is a robot class and also take a look at the importance of this class while testing an application. Moving further we’ll also understand the different methods under this robot class and also we’ll see how to implement this robot class in selenium. And finally, we’ll wrap up this session by taking a look at the limitations of this robot class. 

What is Robot Class & why is it important? 

Time plays a major role while testing and we need to make sure that we complete the desired task within the specific time.  A robot class is used to generate native system input events for the purpose of test automation, self-running demos, and other applications where the control of the mouse and keyboard is needed. The primary purpose of this robot class is to facilitate automation testing for the Java platform implementations. In simple terms, we can say that this class provides control over the mouse and keyboard devices. Robot class can handle the pop-ups during the execution, and the class is very easy to use with the automation process. 

Now you might ask why do we need this robot class when we can perform actions on the keyboard and as well as hover the mouse over the location on the web page? 

Alright to answer your doubt, the Robot class is used to simulate and handle the mouse and keyboard functions. We don’t have to click any button while automating a webpage it can handle all the popups as well as the notification section of the webpage. It also helps you when you want to upload a file onto an application this can be done using this robot class. For instance, if you are trying to download an Email Attachment, a Windows pop-up, ‘Save Attachment’ prompts you to specify the Download Location, appears. It is nothing but a native OS pop-up.   

What are the different methods to implement Robot Class? 

Now let’s move on to our next topic,  what are the different methods that are used while working on this robot class.  So there are basically five different methods.  As we mentioned earlier it handles all the keyboard and mouse functions. Let’s deconstruct them one by one. 


 So the first method would be the key press. This is used to press any key on the keyboard, for example, if you have this particular command it will press the up key in the keyboard. 



Next up, we have the key release method. This is used to release the pressed key of the keyboard for example if you have this particular command it will release the pressed caps lock key in the keyboard. 


So this is about the methods used to control the keyboard functions. Now let’s take a look at the methods that are used to handle the mouse functions.


At first, we have the mouse press method which is used to press the left button of the mouse. So if you implement particular command it helps by pressing the left button of the mouse. 



Next,  we have the mouse release method which is used to release the pressed button of the mouse. 



 Lastly, we have the mouse move method which will move the mouse pointer to the x and the y coordinates of the elements. in this small smooth method, it commands the robot class to move the dot cursor of the mouse to the specified coordinates of the x-axis and y-axis. 


Step-by-Step Implementing the Robot Class

okay so these are the methods that actually control the mouse and the keyboard functions, But to understand how to implement this robot class in selenium, let’s take a look at the implementation of this robot class. Needless to say, we require the latest version of Java installed in our system and also an IDE where we can perform all the actions. 


Before everything,  let’s check if Java is installed in our system.  let’s go to the command prompt and Type

 Java - version

to see if JAVA is installed and the version of JAVA. 

Next,  we need an IDE where we can perform the actions We’re going to consider working on the Eclipse IDE because it is very convenient when you’re working on a Java project. 

>So we’ll just quickly open this Eclipse IDE and launch the workspace. 

>Once you open your Eclipse workspace,  create a new Java Project. 

> You need to give this a name so we’re going to name this project as RobotClass

>click on finish, and you can see that there is a folder being created by the name “robot class.”

> click on the drop-down you can find the source field and the Java libraries. 

>we need to add the selenium jar files to this folder so we’re just going to right-click on this go to build path and configure the selenium libraries. 

> add external jars selenium standalone server

> Make sure all the selenium libraries are added to this project.

 >click on apply and close you can find the referenced folder which holds the selenium libraries.

Creating Code Snippet for robot class in selenium

 Now let’s write our code in this source field, go to new, then class so this a demo class where you’re going to include the main function and click on finish.

In this case, we’re trying to perform actions on our official website at “”; so to do that I’m going to first set the browser trigger by linking the respective browser driver to the ChromeDriver and specify the path.

Let’s see the code snippet, we’re going to use.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.awt.*;
import java.awt.event.KeyEvent;
public class demoClass {
    public static void main(String[] args) throws AWTException, InterruptedException {
        System.setProperty("", "/home/knoldus/Downloads/Ajax handling/browser driver/chromedriver_linux64/chromedriver"); //memory space for your chrome driver
        WebDriver driver = new ChromeDriver();
        driver.get(""); // sample url
        Robot robot = new Robot();  // Robot class throws AWT Exception
        robot.keyPress(KeyEvent.VK_DOWN);// moving the keyboard key down.
        robot.keyPress(KeyEvent.VK_TAB);//pressing the tab key
        robot.mouseMove(30, 100);//moving the mouse.

Output for the above snippet

Now, it’s time to analyze the output of this program.

>It first gets the URL of the web page


 >>Next, it navigates to the next page using the LinkText

>>>Presses the tab on the keyword.

>>>> Now, the output will execute the action performed on the mouse.

Wrapping up

In conclusion, Robot Class in selenium is an AWT package primarily used to generate mouse/keyboard events to interact with OS windows and native apps, however, there are a few disadvantages to using Robot Class in selenium. For instance: Most of the methods like mouseMove are screen resolution dependent so there might be a chance that code working on one machine might not work on another.
Nevertheless, Robot Class is an interactive and fun method if you’re beginning with the Selenium framework & the automated tests project built in Java.

Follow our space on social media platforms for more interesting tutorials & expert-led webinars on the topic of Software Automation & testing. Post-pandemic, there has been a great surge in Automation testing job profiles & companies are always on the look for proficient QA’s and testers. If you wish to make a career in automation, try Zen Class’s Automation Testing w/Selenium course.

Zen Classes’ 5-month Bootcamp in Automation testing is co-developed with some of the top industry leaders in Automation Testing/w Selenium. We harbor a project-based approach so that you develop critical thinking skills based on situations you’d actually encounter in the workplace. 

What you’ll get? 

☑️ A structured, vetted curriculum curated as per your level. 

☑️ Master Selenium, Python, Jenkins, Jmeter, Java & more. 

☑️ 50+ Assured Interview Scheduling.

☑️ EMI Options ( up to 12 months )

Based on your search result, you may be interested in…

What is DOM in Selenium?

How to Automate Captcha in Selenium?

Learn Test Automation from home with these 4 easy steps.

Contact Form

By clicking 'Submit' you Agree to Guvi Terms & Conditions.

Our Learners Work at

Our Popular Course

Share this post

Author Bio

Tushar Vinocha
Tushar Vinocha
pretentious bios will be my 13th reason.

Our Live Classes

Learn Javascript, HTML, CSS, Java, Data Structure, MongoDB & more
Learn Python, Machine Learning, NLP, Tableau, PowerBI & more
Learn Selenium, Python, Java, Jenkins, Jmeter, API Testing & more

UX Processes, Design systems, Responsive UI, & more with placement assistance.

Hey wait, Don’t miss New Updates from GUVI!

Get Your Course Now

Related Articles

IIT Certified Automation Testing with Python

This Certification Course will assist you in procuring a 360-degree view of automation technology.