我正在从事 Liferay 6.2 项目。在 Liferay 中,他们使用了 Vaadin。当我点击一个按钮时,它会打开一个不同的 iframe。我可以编写所有功能的代码。现在我想使用 WebDriver 将光标移动到 iframe 元素。因为当我将鼠标移到 iframe 复选框时,我的自动化脚本就可以运行了。我想自动化脚本以将鼠标指针移动到元素。
我试过下面的代码,但它不起作用。
1) 使用Action moveToElement
:
driver.findElement(By.xpath("element1")).click();
new Actions(driver).moveToElement(driver.findElement(By.xpath("element2"))).click().perform();
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
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);
这行不通。
我只是想将光标点移动到 iframe 定位器。
最佳答案
您可以使用以下方法直接选择 iframe:
driver.switchTo().frame(driver.findElement(By.id("frameId")));
现在,通过使用 selenium 网络驱动程序,您可以在此 iframe 中执行任何操作。
要返回主窗口,您只需:
driver.switchTo().defaultContent();
关于java - 如何使用 WebDriver 移动光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20759348/