如何使用 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.

在使用操作链时,您必须记住像用户那样做".

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();