Skip to main content

Command Palette

Search for a command to run...

Action vs Robot Class

Published
•2 min read•View as Markdown

🖱️ Selenium Actions Class

  • Scope: Operates within the browser, sending commands via WebDriver’s API.

  • Purpose: Simulate high‑level user gestures like click, hover, drag‑and‑drop, and keyboard input—but only on web elements found in the DOM.

  • How it works: Sends “virtual” events to the browser driver, which then fires JavaScript or browser-level events.

  • Use Cases: Web-only interactions:

  • Hover menus

  • Double-clicks

  • Key sequences like Ctrl+C

  • Drag and drop

  •   Actions actions = new Actions(driver);
      actions.moveToElement(menu)
             .click()
             .sendKeys("Hello")
             .perform();
    

    🧑‍💻 Java Robot Class

    • Scope: Operates at the operating system level, controlling the real mouse and keyboard hardware.

    • Purpose: Automate tasks outside the browser, including system dialogs, file upload windows, or desktop apps.

    • How it works: Generates real native OS input events—e.g., moving the actual mouse cursor or sending keystrokes directly to the operating system.

    • Use Cases: Desktop-level actions:

      • OS file upload/download dialogs

      • Authentication pop-ups

      • Installing apps or interacting with native UI

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.mouseMove(500, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);

📊 Comparison Table

FeatureActions ClassRobot Class
LevelBrowser-level (WebDriver API)OS-level (real mouse & keyboard)
TargetsWeb elements (DOM only)Any UI element (browser, OS dialogs, apps)
InteractionVirtual events in browser contextPhysical input—moves cursor, presses keys
Use casesHover, drag‑drop, key sequences, etc.File uploads, OS dialogs, desktop apps
Cross-browserYes (WebDriver proxy)Platform-dependent (screen resolution etc.)

🔑 Key Takeaway

Use Actions when interacting with web elements inside the browser. Use Robot when you need to handle system-level dialogs or desktop interactions that Selenium can't reach.

More from this blog

Command Line Interface

17 posts