Java 如何使用 WebDriver 移动光标位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20759348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:07:38  来源:igfitidea点击:

How to move cursor position using WebDriver

javaseleniumselenium-webdriver

提问by Raj Kumar

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.

我正在从事 Liferay 6.2 项目。在 Liferay 中,他们使用了 Vaadin。当我点击一个按钮时,它会打开一个不同的 iframe。我可以编码所有功能。现在我想使用 WebDriver 将光标移动到 iframe 元素。因为当我将鼠标移动到 iframe 复选框后,我的自动化脚本可以运行。我想自动化一个脚本来将鼠标指针移动到元素。

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

我已经尝试了下面的代码,但它不起作用。

1) using Action moveToElement:

1)使用Action moveToElement

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

2) using mouseMove

2) 使用 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

错误:在((HasInputDevice)驱动程序中出错)。HasInputDevice 无法解析为类型

3)

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

错误:在 getLocationOnScreen() 中出错

4)

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.

我只想将光标点移动到 iframe 定位器。

回答by Innovation

You can directly select iframe using:

您可以使用以下命令直接选择 iframe:

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

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

现在通过使用 selenium web 驱动程序,您可以在此 iframe 中执行任何操作。

To move back to main window you just need to :

要返回主窗口,您只需要:

driver.switchTo().defaultContent();

回答by Curtis Miller

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

您可以使用一个名为 Sikuli 的工具,它直接集成到 Selenium 中:

Sikuli Install Info

Sikuli 安装信息

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

回答by jgode

It's:

它的:

HasInputDevices

(devices is plural)

(设备是复数)

回答by Bey

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

也就是说,您首先找到元素,然后构建一系列要对其执行的操作(在本例中只有一个),然后执行这些操作(现在鼠标应该在元素上)然后单击