Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move cursor position using WebDriver

I am working on a Liferay 6.2 project. In Liferay, they used Vaadin. When I click on a button it opens with a different iframe. I can code that all functionality. Now I want to move the cursor to the iframe element using WebDriver. Because when I move mouse to the iframe checkbox after that my automate script can run. I want to automate a script to move the mouse pointer to the the element.

I have tried the code below, but it doesn't work.

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). HasInputDevice cannot be resolved to a type

3)

Locatable hoverItem = (Locatable) driver.findElement(By.xpath("element xpath"));
int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");

error: getting a error in getLocationOnScreen() The method getLocationOnScreen() is undefined for the type Coordinates

4)

Point coordinates = driver.findElement(By.xpath("element xpath")).getLocation();
Robot robot = new Robot();
WebElement markNews = driver.findElement(By.xpath("element xpath"));
markNews.click();
robot.mouseMove(coordinates.x,coordinates.y+80);

This does not work.

I just want to move cursor point to the iframe locator.

like image 212
Raj Kumar Avatar asked Apr 08 '26 06:04

Raj Kumar


2 Answers

You can directly select iframe using:

driver.switchTo().frame(driver.findElement(By.id("frameId")));

Now by using selenium web driver you can perform any operation in this iframe.

To move back to main window you just need to :

driver.switchTo().defaultContent();
like image 170
Innovation Avatar answered Apr 19 '26 16:04

Innovation


It's:

HasInputDevices

(devices is plural)

like image 30
jgode Avatar answered Apr 19 '26 16:04

jgode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!