How to Mouse Hover an element in Selenium WebDriver
Selenium Tutorial for Beginners

How to Mouse Hover an element in Selenium WebDriver

Continuing on our Selenium Beginner’s series, let’s see how to Mouse Hover an element in Selenium WebDriver (a menu) and then click on a Sub-menu in Selenium WebDriver using Java. Hope you have already gone through the previous tutorials which will be extended here…

#4. Common Page elements & User actions to automate

#6. How to click a Button in Selenium WebDriver using Java

Here we will try to mouse hover the ‘Electronics’ menu on Flipkart.com and then click on ‘Samsung’ from the drop-down sub-menu.

Mouse Hover an Element in Selenium WebDriver

Inspect the Element first

This can be done by using browser tools. In simple terms just right click on the element (‘Electronics’ menu item) & select ‘Inspect Element’. Note down the ‘attribute-value’ pairs, in this case class & title.

Inspect Element HTML in Selenium WebDriver

“Electronics” Menu

“Samsung” Sub-menu

<< THE CODE >>

The steps followed to Mouse Hover an element in Selenium WebDriver,

  1. Open a browser
  2. Open the website URL & maximize the window
  3. Find the ‘Electronics’ menu and Mouse hover it
  4. Identify the sub-menu ‘Samsung’ and ‘Click’ on it.

 

Open Browser & Application

The code till,

is to open the browser and application. For reference, please visit our earlier posts in the series.

Identify the Menu & Sub-Menu element

One can identify the element using different properties. Here we have identified these two elements using the xpath property using ‘title’ attribute. For details on different element locators, click here…

After identifying the menu elements, we are saving it in the ‘MainMenu’ & ‘SubMenu’ objects of type ‘WebElement’ which is an in-built class available in Selenium. We need to import this class in order to create objects,

Mouse hover Main Menu & click on the Sub Menu

Selenium WebDriver provides a user-facing API for emulating complex user gestures – public class Actions extends java.lang.Object. In order to use objects of this class, we need to import it first,

This class is used to automate different keyboard and mouse actions like SendKeys, Mouse Hover, Right-click, Double-click, Drag & Drop, etc. First we need to create a new Actions builder instance by passing the WebDriver instance,

To Mouse Hover an element in Selenium WebDriver, the method used is ‘moveToElement’ – moves the mouse to the middle of the element. Actions class implements the builder pattern: Builds a CompositeAction containing all actions specified by the method calls. Using the Actions object, we first move to the ‘MainMenu’ element, i.e. mouse hover. It will reveal the subsequent sub-menu.

Here build() method is used to generate a composite action containing all actions so far, ready to be performed (and it resets the internal builder state, so subsequent calls to build() will contain fresh sequences). perform() method is a convenience method for performing the actions. After the sub-menu is exposed, we just need to ‘click’ (clicks in the middle of the given element) on our identified sub-menu.

Why WebDriverWait

We will have a separate post to cover the use of ‘Wait’ in automation scripting. Before that let’s get an idea why did we use WebDriverWait here,

In simple terms – WebDriverWait will wait for a certain time or certain condition to be true before the execution proceeds. Here we have used it because ‘SubMenu’ item can only be clicked after mouse hover ‘MainMenu’ element. Hence after mouse hover MainMenu it’s better to wait for the SubMenu element to be clickable to execute further statements.

The Result

Mouse Hover an element in Selenium WebDriver | Once you run this program it will open the browser >> open the Flipkart URL >> Mouse-hover the ‘Electronics’ Main menu >> Click on the ‘Samsung’ sub-menu element.

Save

Save

Save

Save

Save

Save

Save

Save

Save

Leave a Reply

Your email address will not be published. Required fields are marked *