Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
15.5k views
by The go-to Tester (391 points)
How may I move the mouse cursor to a given position in the page using Selenium Webdriver?

3 Answers

1 like 0 dislike
by Contributing Tester (31 points)

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

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} span.s1 {color: #7e504f} span.s2 {color: #931a68} span.s3 {color: #0326cc} span.s4 {color: #3933ff} span.Apple-tab-span {white-space:pre}

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

0 like 0 dislike
by The go-to Tester (181 points)
Moving mouse cursor is something which can not be done. However you can simulate the action using below methods.
 
1) using Action moveToElement :
driver.findElement(By.xpath("element1")).click();
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();
mouse.mouseMove(hoverItem.getCoordinates());
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();
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
 
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"));
markNews.click();
robot.mouseMove(coordinates.x,coordinates.y+80);
 
 
Hope that helps
0 like 0 dislike
by
Use Robot class in Java.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...