top of page
Search

How to click on an element using Robot Class in selenium WebDriver

  • Writer: Dev Raj Sinha
    Dev Raj Sinha
  • Nov 2, 2023
  • 2 min read

Using the `Robot` class in Java, you can simulate keyboard and mouse actions. To click on an element using the `Robot` class in Selenium WebDriver, you can follow these steps:


1. Locate the Element:

First, locate the element you want to click using standard Selenium WebDriver locators like ID, class name, XPath, etc.


   WebElement element = driver.findElement(By.id("elementId"));

2. Get the Coordinates of the Element:

Get the X and Y coordinates of the element you want to click. You can use the `getLocation()` method of the `WebElement` interface.


   Point coordinates = element.getLocation();
   int x = coordinates.getX();
   int y = coordinates.getY();

3. Use the Robot Class to Simulate Mouse Click:

Create an instance of the `Robot` class and use the `mouseMove()` method to move the mouse to the element's coordinates, then use the `mousePress()` and `mouseRelease()` methods to simulate a mouse click.


   Robot robot = new Robot();
   robot.mouseMove(x, y);  // Move the mouse to the element's coordinates
   robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);  // Press the left mouse button
   robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);  // Release the left mouse button

Here's the complete example:


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

import java.awt.*;
import java.awt.event.InputEvent;

public class ClickElementWithRobot {
    public static void main(String[] args) throws AWTException {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");

        // Locate the element you want to click
        WebElement element = driver.findElement(By.id("elementId"));

        // Get the coordinates of the element
        Point coordinates = element.getLocation();
        int x = coordinates.getX();
        int y = coordinates.getY();

        // Use Robot class to simulate mouse click
        Robot robot = new Robot();
        robot.mouseMove(x, y);  // Move the mouse to the element's coordinates
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);  // Press the left mouse button
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);  // Release the left mouse button
    }
}

Remember to replace `"path/to/chromedriver.exe"` with the actual path to your ChromeDriver executable and `"elementId"` with the ID of the element you want to click. Also, handle `AWTException` by adding it to the `throws` clause or using a try-catch block. Note that using the `Robot` class should be your last resort if other methods for clicking the element do not work, as it's less reliable and can lead to flaky tests.

 
 
 

Recent Posts

See All

Comments


Never Miss a Post. Subscribe Now!

Find new exciting ways to dominate the IT Automation industry

Thanks for submitting!

bottom of page