3

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.

4 Answers 4

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

i dont want to switch frame. i have already done that stuff. i need to move cursor(mouse pointer).
If your site support jquery then you can try selenium javascript executor with this code $(document) .mouseXPos(e.pageX + 50) .mouseYPos(e.pageY + 50);
1

It's:

HasInputDevices

(devices is plural)

Comments

1

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

Comments

0

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.