</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What is the Robot class in Java and when do you use it in Selenium?

Answer

Robot Class in Selenium

java.awt.Robot simulates native operating system keyboard and mouse events — not browser-level like Selenium. It is used when WebDriver cannot reach OS-level pop-ups.

When to Use Robot Class

  • OS file upload dialogs (not HTML input)
  • Windows authentication pop-ups
  • Print dialogs
  • Keyboard shortcuts at OS level (Win key, Alt+F4)
  • Moving/clicking outside browser window

Setup and Basic Usage

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

Robot robot = new Robot();

Handle OS File Upload Dialog

Java
// Click button that opens OS file upload dialog
driver.findElement(By.id("uploadBtn")).click();
Thread.sleep(1000); // wait for OS dialog to appear

// Type file path using clipboard (handles special chars/spaces)
StringSelection filePathSelection = new StringSelection("C:\\test\\sample.pdf");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filePathSelection, null);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);    // Ctrl+V to paste
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

Thread.sleep(500);
robot.keyPress(KeyEvent.VK_ENTER); // Press Enter to confirm
robot.keyRelease(KeyEvent.VK_ENTER);

Keyboard Shortcuts

Java
// Press Tab key
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);

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

// Press Escape
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);

// Ctrl+A (Select All)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);

// Alt+F4 (Close window)
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);

Mouse Operations

Java
// Move mouse to specific screen coordinates
robot.mouseMove(500, 300);

// Left click
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

// Right click
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

Important Limitations

  • Works only when tests run on a machine with a display (not headless servers)
  • Not cross-platform reliable — coordinate-based clicks depend on screen resolution
  • Does not work inside Docker containers or CI without virtual display (Xvfb on Linux)
  • Prefer sendKeys() on <input type="file"> for file uploads over Robot when possible

Robot vs Actions vs JavascriptExecutor

CapabilityRobotActionsJavascriptExecutor
OS dialogs
Browser elements
Keyboard shortcuts✓ (OS level)✓ (browser)
Drag & drop
Headless support

Follow AutomateQA

Related Topics