1

I want to use the Java Robot class in order to move the mouse over a link to dynamically create more content. For the web interactions I use the Selenium WebDriver.

    Point coordinates = driver.findElement(By.xpath("//li[@id='1234']/a")).getLocation();
    Robot robot;
    try {
        robot = new Robot();
        robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
    } catch (AWTException e1) {
        e1.printStackTrace();
    }

Selenium throws an error for the getLocation function:

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot determine size of element

Does anybody know what am I doing wrong?

3 Answers 3

1

mouseover action you can achieve (Actions class) without using Robot also.

new Actions(driver).moveToElement(driver.findElement(By.xpath("//li[@id='1234']/a"))).perform();

include below import statement in your file.

import org.openqa.selenium.interactions.Actions;
1
  • 2
    Sorry but I need Robot because Actions is not working in my case.
    – cruxi
    May 28, 2014 at 15:26
1

If you just want to make a mouse movement on the page, Selenium interactions can help you do the same.

Here is the sample code for you

WebElement myLink = driver.findElement(By.xpath("//li[@id='1234']/a"));

Actions act = new Actions(driver);
act.moveToElement(myLink).build().perform();

// if you want to click on the link : 
act.click(myLink).build().perform();

// if you want to move to the element and then click onthe link : 
act.moveToElement(myLink).click(myLink).build().perform();

// or can be done in two different steps like this : 
act = act.moveToElement(myLink);
act.click(myLink).build().perform()

For doing this we should import org.openqa.selenium.interactions.Actions;

Hope this solves your problem.

0

I tried this and it seems to work for me. Please check

Point p = webele.getLocation();
int x = p.getX();
int y = p.getY();
Dimension d = webele.getSize();
int h = d.getHeight();
int w = d.getWidth();
Robot r = new Robot();
r.mouseMove(x + (w/2), y+(h/2) +80);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.