0
0
Selenium Javatesting~5 mins

Mouse hover (moveToElement) in Selenium Java

Choose your learning style9 modes available
Introduction

Mouse hover helps you test what happens when you move the mouse over an element without clicking. It shows hidden menus or tooltips.

To check if a dropdown menu appears when you hover over a menu item.
To verify tooltip text shows up when hovering on an icon.
To test if buttons change style or color on mouse hover.
To ensure hover-triggered popups or images appear correctly.
To simulate user behavior on interactive web elements.
Syntax
Selenium Java
Actions actions = new Actions(driver);
actions.moveToElement(WebElement element).perform();
Use Actions class to create complex user gestures like mouse hover.
Always call perform() to execute the action.
Examples
Hover over an element found by its ID 'menu'.
Selenium Java
WebElement menu = driver.findElement(By.id("menu"));
Actions actions = new Actions(driver);
actions.moveToElement(menu).perform();
Hover over an element with class 'info-icon' using a one-liner.
Selenium Java
WebElement icon = driver.findElement(By.cssSelector(".info-icon"));
new Actions(driver).moveToElement(icon).perform();
Hover over a button with text 'Submit' using XPath locator.
Selenium Java
WebElement button = driver.findElement(By.xpath("//button[text()='Submit']"));
Actions actions = new Actions(driver);
actions.moveToElement(button).perform();
Sample Program

This test opens a page, hovers over a menu item, and checks if the submenu appears. It prints PASS if visible, else FAIL.

Selenium Java
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;

public class HoverTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://www.example.com/menu");
            WebElement menu = driver.findElement(By.id("menu-item"));
            Actions actions = new Actions(driver);
            actions.moveToElement(menu).perform();
            // After hover, check if submenu is displayed
            WebElement submenu = driver.findElement(By.id("submenu-item"));
            if (submenu.isDisplayed()) {
                System.out.println("PASS: Submenu is visible on hover.");
            } else {
                System.out.println("FAIL: Submenu is not visible on hover.");
            }
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Make sure the element is visible and interactable before hovering.

Hover may not work if the element is off-screen; scroll into view first if needed.

Use explicit waits to handle dynamic content after hover.

Summary

Mouse hover simulates moving the mouse pointer over an element.

Use Selenium's Actions class with moveToElement() and perform() methods.

It helps test UI changes triggered by hovering, like menus or tooltips.