Our objective in this small project is to do a mouse over on a particular link. We are not suppose to actually click it, only to hover our move on it.

We are going to use Actions available in Selenium and chain them together since there is no separate “move hover/ mouse over” action available in Selenium.

We will open a company’s website. Hover over the “About Us” option and select “Our Team” from the dropdown.

Here the complication is, you cannot navigate to “Our Team” page without opening the dropdown or directly using the url.

Our steps will be:

  1. Open “www.spartez.com”
  2. Hover on “About” option in the nav menu.
  3. Click on “Our Team” from the dropdown.

Project Setup

As always, the good practice to start a project is to start it as a Maven project. This way we can quickly get the dependencies in the project which we need. Add following dependencies to your pom.xml file.


Always make sure that Auto Import is enabled for Maven.


Create a java class file MouseOverTest.java. This will contain our test. Add following code to it.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MouseOverTest {
    WebDriver driver;
    Actions action;

    public void setup(){
        System.setProperty("webdriver.chrome.driver", "pathtochromedriver/chromedriver.exe");
        driver = new ChromeDriver();
        action = new Actions(driver);

    public void Test1(){
        WebElement about = driver.findElement(By.xpath(".//*[@id='page-header']/div/nav/ul/li[2]/a"));

    public void tearDown(){

Here, we have used Actions, and created an object “action” for it. It needs to initialized with the WebDriver else you’ll end up with Null Pointer exception.

Next we identify the “About Us” button and store as a WebElement.

Next we use the action “moveToElement” to navigate to the WebElement and “perform” to execute the action. This way, the dropdown menu will be invoked.

Last step is to select the “Our Team” option from the menu and we are done!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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