Java 鼠标悬停并随后单击菜单项
在headless模式下使用SeleniumWeb驱动程序API,我试图在菜单项上执行鼠标悬停;使所有子菜单项都显示出来。然后,单击其中一个子菜单项。以下是代码片段:Java 鼠标悬停并随后单击菜单项,java,selenium-webdriver,phantomjs,Java,Selenium Webdriver,Phantomjs,在headless模式下使用SeleniumWeb驱动程序API,我试图在菜单项上执行鼠标悬停;使所有子菜单项都显示出来。然后,单击其中一个子菜单项。以下是代码片段: Actions actions = new Actions(driver); WebDriverWait wait = new WebDriverWait(driver, 20); //Waiting for menu item to be clickable wait.until(ExpectedConditions.elem
Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 20);
//Waiting for menu item to be clickable
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(menu_item)));
WebElement firstLink = driver.findElement(By.cssSelector(menu_item));
//Hovering over the menu item
actions.moveToElement(firstLink).build().perform();
//Waiting for the sub-menu item to be clickable - ERRORS OUT HERE
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(submenu_item)));
//Getting the second link
WebElement secondLink = driver.findElement(By.cssSelector(submenu_item));
//Click on sub menu
actions.moveToElement(secondLink).click().build().perform();
但是,测试超时,表示找不到子菜单项
我已经验证了上述功能在Firefox驱动程序中运行得非常好。
而且,css选择器是正确的
PhantomJs和鼠标悬停操作是否存在任何已知问题?这可能解释了为什么代码不能与PhantomJs驱动程序一起运行。如果是的话,是否有类似的解决办法
另外,是否有更好的方法对API排序,以模拟鼠标悬停,然后单击操作
(注意:我还尝试链接中提到的操作,但没有帮助)尝试使用非本机JavascriptExecutor:
public void mouseHoverJScript(WebElement HoverElement) {
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,
HoverElement);
}
我认为PhantomJS还不支持这一点。你找到解决办法了吗?简单回答:Javascript没有被执行!