Selenium WebDriver has a provision to perform different mouse operations like Mouse hover, Right Click, Double-click, Drag and Drop etc. In this tutorial, we will be covering Mouse Hover. You might have seen this operation on e-commerce websites where if we move to an item of the menu and hold the mouse pointer there, a sub menu pops-up. To interact with the elements of sub-menu, again we have to move to that element and then perform the operation.

Let’s cover this topic with the help of an example:

  1. Open browser(Consider Chrome for this topic)
  2. Invoke URL(“”)
  3. Hover mouse over “Mobile & Accessories” on the homepage.
  4. Hover mouse over “Sony Mobiles” and then click on it.

Here is the sample code for the above scenario-

public class MouseHover {

public static void main(String[] args) {

System.setProperty("", "C:\\Users\\sdhingra\\workspace\\libs\\chromedriver.exe");

ChromeDriver Driver = new ChromeDriver();

String url = "";



Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Actions action = new Actions(Driver);

WebElement mobileElement = Driver.findElement(By.linkText("Mobile & Accessories"));


WebElement sonyElement = Driver.findElement(By.linkText("Sony Mobiles"));





EXPLANATION- Initial few lines of code would remain same i.e. launching browser of your choice say chrome, maximising the window, deleting all cookies etc.

IMPORTANT NOTE-  If you notice we have added an additional line of code before invoking the URL.

Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

This is the practical implementation of implicit wait that we had covered in one of our important topics i.e. Wait in Selenium WebDriver .This implicit wait will put a wait of “20 seconds” before executing each and every line of the test script in the above scenario. Later, moving ahead in the script we will see the importance of putting wait in any script.

Let’s discuss each and every line of code in detail after invoking the URL-

Example of Mouse hover action

                                                                                                            Example of Mouse hovers action

  • Actions action = new Actions(Driver);

-> Selenium package offers “Actions” class to perform mouse operations in Selenium WebDriver.We have created an instance of Actions class i.e. “action” to access methods of Actions class later in the script.

  • WebElement mobileElement = Driver.findElement(By.linkText(“Mobile & Accessories”));

-> Here, we are locating “Mobile & Accessories” link on the homepage with the help of “linkText” locator and storing  it in a WebElement “mobileElement”.

  • action.moveToElement(mobileElement).build().perform();

-> This command will move the mouse pointer to a WebElement(mobileElement) here. Method used is moveToElement()Later,we have build the command and perform this action. In short, mouse will be hovered on “Mobile & Accessories” in this line of code.

  • WebElement sonyElement = Driver.findElement(By.linkText(“Sony Mobiles”));

-> In this line of code we are locating “Sony Mobiles” with the help of “linkText” locator and storing its value in WebElement “sonyElement”. This new WebElement appears as a sub-menu after mouse hovering over “Mobile & Accessories”.

IMPORTANT NOTE- If we remove the implicit wait that we had put before invoking the URL.This line of code will fail. As, when we hover the mouse over “Mobile & Accessories” it takes some time to load all the available options and script could fail with “Element not found” exception.While trying this scenario, once execute the code without putting wait condition and later with wait condition implemented.The difference could be related easily.

  • action.moveToElement(sonyElement).click().build().perform();

-> In this line of code, we are simply moving the mouse cursor to “Sony Mobiles” and later clicking on it.This could have been done in the other way around too.Like locating “Sony Mobiles” and later used order to understand mouse operations we are first moving to the webElement and later clicking on it.

So, this was all about mouse operations in Selenium WebDriver.Hope you understood..!!

For any questions, queries or comments feel free to write us at or Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: