Using selenium web-driver API in headless mode, I am trying to perform a mouse-hover on a menu item; so that all the sub menu items show up. After that, I click one of the sub menu items. The following is the code snippet:
在无头模式下使用selenium web-driver API,我试图在菜单项上执行鼠标悬停;以便显示所有子菜单项。之后,我单击其中一个子菜单项。以下是代码段:
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();
However, the test times out saying that the sub menu item could not be found.
但是,测试超时说无法找到子菜单项。
I've verified that the above works perfectly fine with the Firefox driver. Also, the css selectors are proper.
我已经验证上面的Firefox驱动程序完全正常。此外,css选择器是正确的。
Are there any known issues with PhantomJs and mouse hover actions - which might explain why the code does not run with the PhantomJs driver. If so, is there any work-around for the same.
PhantomJs和鼠标悬停操作是否存在任何已知问题 - 这可能解释了为什么代码不能与PhantomJs驱动程序一起运行。如果是这样,是否有相同的解决方法。
Also, is there a better way to sequence the APIs to simulate the mouse-hover-and-then-click action?
此外,是否有更好的方法来对API进行排序以模拟鼠标悬停然后单击操作?
(Note: I also tried chaining the operations as mentioned in How to perform mouseover function in Selenium WebDriver using Java?, but it didn't help)
(注意:我也尝试过链接如何使用Java在Selenium WebDriver中执行鼠标悬停功能中提到的操作,但它没有帮助)
1 个解决方案
#1
0
Try using non-native JavascriptExecutor:
尝试使用非本机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);
}
#1
0
Try using non-native JavascriptExecutor:
尝试使用非本机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);
}