In order to perform a 'mouse hover' action, we need to chain all of the actions that we want to achieve in one go. So move to the element that which has sub elements and click on the child item. It should the same way what we do normally to click on a sub menu item.
With the actions object you should first move the menu title, and then move to the sub menu item and click it.
First we need to create new action builder instance by passing the webdriver instance, then.
Below is the sample code to perform Mouse hover action
Example 1:
Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);
WebElement subMenu = driver.findElement(By.cssSelector("subLinklocator"));
actions.moveToElement(subMenu);
actions.click().build().perform();
Here 'build()' method is used to compile all the list of actions into a single step and ready to be performed.
Example 2:
Actions action = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("MainMenu"));
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("submenuxpath"))).click().build().perform();
There are cases where you may just want to mouse hover on particular element and check if the button state/color is changing after mouse hover.
Below is the example to perform mouse hover
WebElement searchBtn = driver.findElement(By.id("searchbtn"));
Actions action = new Actions(driver);
action.moveToElement(searchBtn).perform();
Click here for more mouse hover methods, keyboard events and examples
Comments
Known issue in Selenium 3.x
Known issue. Actions still not working with Selenium 3.4.0
Mouse Hover
Your Code is very helpful, thank you so much.
Mouse Hover
How to check if the color of link is changing after the mouse hover action?
Mouse Action
HI
I am facing one issue, while performing mouse hover action.
While script is running mouse hover action, if we suddenly move the system mouse manually then script getting failed
how to handle this, while performing mouse over action, if we move the system mouse my script should not fail
Thanks,
Laki
Add new comment