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.

Below is the sample code to perform Mouse hover action

Example 1:

Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText(“menulink”));

WebElement subMenu = driver.findElement(By.cssSelector(“subLinklocator”));

Example 2:

Actions action = new Actions(webdriver);
WebElement mainMenu = webdriver.findElement(By.linkText(“MainMenu”));
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(“searchbtn”));

Actions action = new Actions(driver);

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s