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;
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry but I need Robot because Actions is not working in my case.
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.

Comments

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);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.