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?
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;
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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.