如何使用WebDriver移动光标位置 [英] How to move cursor position using WebDriver
问题描述
我正在研究Liferay 6.2项目.在Liferay中,他们使用了Vaadin.当我点击一个按钮时,它会以不同的iframe打开.我可以编写所有功能的代码.现在,我想使用WebDriver将光标移动到iframe元素.因为当我将鼠标移到iframe复选框之后,我的自动化脚本可以运行.我想自动化一个脚本,将鼠标指针移到元素上.
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)使用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());
错误:在((HasInputDevice)驱动程序)中出现错误. HasInputDevice无法解析为类型
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+");");
错误:getLocationOnScreen()中出现错误 对于坐标类型,未定义方法getLocationOnScreen()
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定位器上.
I just want to move cursor point to the iframe locator.
推荐答案
我想你的第一个例子:
new Actions(driver).moveToElement(driver.findElement(By.xpath("element2"))).click().perform();
应阅读:
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
这篇关于如何使用WebDriver移动光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!