Hover over the menu item.

Using the selenium web-driver API in headless mode, I'm trying to mouse over a menu item; so that all submenu items are displayed. After that, I click one of the submenu items. The following is a snippet of code:

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 expires, saying that the submenu item was not found.

I have verified that the above works fine with the Firefox driver . Also, css selectors are correct.

Are there any known issues with the actions of PhantomJs and mouse hover that might explain why the code does not work with the PhantomJs driver. If so, is there any work for this.

Also, is there a better way to arrange APIs to simulate mouse-hover actions and subsequent clicks?

(Note. I also tried to bind the operations mentioned in How to execute the mouseover function in Selenium WebDriver using Java? But this did not help)

+4
source share
1 answer

Try using a non-native 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); } 
0
source

Source: https://habr.com/ru/post/1497164/


All Articles