How may I move the mouse cursor to a given position in the page using Selenium Webdriver?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

3 Answers

by (31 points)
+1 vote

Here is a piece of code where we can move the mouse pointer to a location and clicking on the moved region

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

Actions builder = new Actions(driver);  

WebElement Element = driver.findElement(By.xpath("//div[@id='signUpModal']")); 

builder.moveToElement(Element, 100, 100).click().build().perform();

Note: The Pointer will move with reference to region of element described "Element"(Top left of Element region is considered as 0,0).

by (171 points)
0 votes
Moving mouse cursor is something which can not be done. However you can simulate the action using below methods.
1) using Action moveToElement :
new Actions(driver).moveToElement(driver.findElement(By.xpath("element2"))).click().perform();
2) using mouseMove
WebElement element = driver.findElement(By.xpath("element xpath"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevice) driver).getMouse();
error: getting a error in ((HasInputDevice) driver). 
3)Using Locatable class 
Locatable hoverItem = (Locatable) driver.findElement(By.xpath("element xpath"));
int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
4) Using Point and Robot classes
Point coordinates = driver.findElement(By.xpath("element xpath")).getLocatio();
Robot robot = new Robot();
WebElement markNews = driver.findElement(By.xpath("element xpath"));
Hope that helps
0 votes
Use Robot class in Java.