如何使用Java在Selenium WebDriver中执行鼠标悬停功能?

如何使用Java在Selenium WebDriver中执行鼠标悬停功能?

问题描述:

我想在下拉菜单中执行鼠标悬停功能。当我们将鼠标悬停在菜单上时,它会显示新选项。
我试图使用xpath单击新选项。但无法直接单击菜单。
所以,作为手动方式,我试图将鼠标悬停在下拉菜单上,然后点击新选项。

I want to do mouseover function over a drop down menu. When we hover over the menu, it will show the new options. I tried to click the new options using the xpath. But cannot click the menus directly. So, as the manual way i am trying to hover over the drop down menu and then will click the new options.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).build().perform();


实际上不可能执行'鼠标悬停'操作,而是需要将您想要一次完成的所有操作链接起来。所以移动到显示其他元素的元素,然后在同一个链中,移动到现在显示的元素并单击它。

Its not really possible to perform a 'mouse hover' action, instead you need to chain all of the actions that you want to achieve in one go. So move to the element that reveals the others, then during the same chain, move to the now revealed element and click on it.

使用Action Chains时你必须记住像用户那样做。

When using Action Chains you have to remember to 'do it like a user would'.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();