第一种悬停方法:
WebElement menu1=driver.......
WebElement menu2=driver.......
Actions builder = new Actions(driver);
Actions hoverOverRgeistrar = builder.moveToElement(menu1);
hoverOverRgeistrar.perform();
menu2.click();
// builder.moveToElement(element).perform();
注:不过此方法不能完全模拟鼠标悬停,而且会导致二级菜单闪现,需要用到JavaScript
第二种悬停方法:
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);
}