Raj Kumar
Raj Kumar

Reputation: 41

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.

Upvotes: 3

Views: 44458

Answers (4)

Bey
Bey

Reputation: 31

I thought your first example:

new Actions(driver).moveToElement(driver.findElement(By.xpath("element2"))).click().perform();

Should read:

new Actions(driver).moveToElement(driver.findElement(By.xpath("element2"))).build().perform().click();

That is, you first find the element, then build a series of actions to perform on it (in this case just one) then perform those actions (now the mouse should be over the element) then click

Upvotes: 1

jgode
jgode

Reputation: 1869

It's:

HasInputDevices

(devices is plural)

Upvotes: 1

Curtis Miller
Curtis Miller

Reputation: 578

You can use a tool called Sikuli which integrates directly into Selenium:

Sikuli Install Info

Example Code:

import org.sikuli.script.Button;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Location;
import org.sikuli.script.Match;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

Screen screen = new Screen();
// Find where you want to move the mouse and set a location
Location wheelPoint = new Location(1000, 800);
// You can always just get center as well
// Location wheelCenter = screen.getCenter();
try {
    screen.wheel(wheelPoint, Button.WHEEL_DOWN, steps);
} catch (Exception e) {
    Assert.fail("Mouse did not move to desired location");
}

Upvotes: 0

Innovation
Innovation

Reputation: 1534

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

Upvotes: 1

Related Questions