第一种悬停方法:
action.clickAndHold();// 鼠标悬停在当前位置,既点击并且不释放
action.clickAndHold(onElement);// 鼠标悬停在 onElement 元素的位置
不过clickAndHold不能完全模拟鼠标悬停,而是会导致二级菜单的闪现,需要用js
第二种悬停方法:
public static void MouseHoverByJavaScript(WebElement targetElement,WebDriver driver) { String mouseHoverjs = "var evObj = document.createEvent('MouseEvents');" + "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" + "arguments[0].dispatchEvent(evObj);"; JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript(mouseHoverjs, targetElement); }