How to use Robot Class in Selenium

  

The Robot Class in Java belongs to the java.awt package. It is used in Selenium to handle keyboard and mouse events that cannot be handled using the WebDriver or Actions class such as file upload windows, OS-level popups, or simulating keyboard shortcuts like Ctrl+S, Alt+Tab, etc.


Use Cases of Robot Class

  • Handling file upload dialogs (OS windows)

  • Simulating keyboard shortcuts (example: Ctrl+C, Enter)

  • Performing mouse movements or clicks outside the browser

  • Dealing with download popups, print popups, etc.


Common Robot Class Methods

MethodDescription
keyPress(int keycode)Presses a key
keyRelease(int keycode)Releases a key
mouseMove(int x, int y)Moves mouse pointer to (x, y) screen coordinates
mousePress(int buttons)Presses mouse buttons
mouseRelease(int buttons)Releases mouse buttons
delay(int ms)Delays execution for specified milliseconds


Uses KeyEvent.VK_* constants for keys, e.g., KeyEvent.VK_ENTER, KeyEvent.VK_CONTROL.


File upload example: Selenium Script Using Robot Class 

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class RobotClassFileUploadExample {
    public static void main(String[] args) throws AWTException, InterruptedException {
        // Set path to chromedriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize driver
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        // Open sample file upload site
        driver.get("https://www.filemail.com/share/upload-file");

        // Click on "Add Files" button to open OS file upload dialog
        driver.findElement(By.xpath("//button[text()='Add Files']")).click();

        // Wait for file upload window to appear
        Thread.sleep(2000);

        // Set the file path to clipboard
        StringSelection filePath = new StringSelection("C:\\Users\\YourUser\\Documents\\sample.txt");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filePath, null);

        // Use Robot to press CTRL+V and Enter
        Robot robot = new Robot();
        robot.delay(1000);

        // Press CTRL + V
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);

        robot.delay(500);

        // Press Enter
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

        // Done!
        Thread.sleep(3000);
        driver.quit();
    }
}


Suppose we are uploading a file to a form, and we need to handle the OS file chooser window:



Explanation:

  • StringSelection + Toolkit.getDefaultToolkit().getSystemClipboard() → Puts file path into clipboard.

  • Robot simulates Ctrl+V and Enter to paste the path and upload the file.


Important points:

  • Robot class interacts outside of browser context, so it can handle system-level dialogs.

  • Always add Thread.sleep() or robot.delay() to ensure the timing is synced properly with the UI events.