Mouse Hover Actions in Selenium Webdriver

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

Selenium Tutorials: 

Comments

Thanks for the code of mouse over actions it helped me

I know that's very silly question , but can anyone let me know why we are we using driver in brackets while making Action object
Actions action = new Actions(driver);

driver is the reference variable of webdriver element . So using webdriver only we opening browser for that you taking reference varaible as driver in action.

I tried the possible ways to select the sub menu from main menu. but i could not find the solution. and I tried the given above example code too.

Please if u find the answer let me know

upload a file will not possible using selenium,we need use robot class or autoIt

could not select a month from Birthday field while signup to gmail. Please help me regarding.

How to check, the mouse hover action is performed or not. Is Mouse is hovered above the Web element??

We are passing driver inside actions constructor for actions class to perform on which ever Brower the object driver is created

Hi,
Mouse hover - move to element is not working for me.
i am using ff - 49.0.1 amd selenium - 3.0.1.
can you pls help on dis?
Thanks

Hii guys im trying to handle component art dropdowns but not succeeded so far any method that you guys can think of or have used

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.