Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Using Selenium WebDriver in Java, you can perform various mouse actions, including hovering over an element. Hovering over an element often triggers certain events, such as displaying a dropdown menu or showing tooltips. The ‘Actions‘ class in Selenium WebDriver provides a way to perform advanced user interactions like mouse hover.

1. Set Up Your Project

First, ensure you have the necessary dependencies. If you’re using Maven, add the following dependencies to your ‘pom.xml‘ file:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

				
			

2. Import Required Classes

You need to import several classes to use the ‘Actions‘ class and perform a mouse hover:

				
					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.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

				
			

3. Set Up WebDriver and Navigate to the Page

Initialize the WebDriver and navigate to the desired webpage:

				
					public class MouseHoverExample {
    public static void main(String[] args) {
        // Set the path of the WebDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to the web page
        driver.get("http://example.com");

        // Maximize the browser window
        driver.manage().window().maximize();
        
        // Add a wait time
        WebDriverWait wait = new WebDriverWait(driver, 10);
        
        // Locate the element to hover over
        WebElement hoverElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("hoverElementId")));

        // Perform the hover action
        Actions actions = new Actions(driver);
        actions.moveToElement(hoverElement).perform();

        // Add additional actions if needed
        // For example, clicking a submenu that appears after hovering
        WebElement subMenuElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("subMenuElementId")));
        actions.moveToElement(subMenuElement).click().perform();

        // Close the browser
        driver.quit();
    }
}

				
			

4. Explanation of the Code

  • Set up the WebDriver: Initialize the ChromeDriver and navigate to the desired URL.
  • Locate the Element: Use ‘WebDriverWait‘ and ‘ExpectedConditions‘ to wait until the element is visible. This ensures that the element is present and visible on the page before performing any actions.
  • Perform the Hover Action: Use the ‘Actions‘ class to move the mouse to the target element. The ‘moveToElement‘ method is used to perform the hover action.
  • Additional Actions: If you need to interact with any elements that appear after hovering, you can chain more actions, like clicking on a submenu.
  • Close the Browser: Always remember to close the browser at the end of the test to free up resources.

5. Running the Code

Ensure that the path to the ChromeDriver executable is correctly set in your system. You can download the ChromeDriver from the official site.

Once the setup is complete, you can run your Java program. The WebDriver will open the specified webpage, hover over the target element, perform any additional actions, and then close the browser.

Example with an Actual Webpage

Let’s consider an example where we hover over a menu item on a webpage and then click on a submenu that appears:

				
					public class MouseHoverExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.get("https://www.yourwebsite.com");
        driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver, 10);

        // Locate the menu item to hover over
        WebElement menuElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("cssSelectorOfMenuElement")));

        // Perform the hover action
        Actions actions = new Actions(driver);
        actions.moveToElement(menuElement).perform();

        // Wait and locate the submenu item
        WebElement subMenuElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("cssSelectorOfSubMenuElement")));
        actions.moveToElement(subMenuElement).click().perform();

        driver.quit();
    }
}

				
			

In this example, replace ‘"cssSelectorOfMenuElement"‘ and ‘"cssSelectorOfSubMenuElement"‘ with the actual CSS selectors of the elements you want to interact with.

This should give you a comprehensive understanding of how to perform mouse hover actions using Selenium WebDriver in Java.

Scroll to Top