πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 24 of 100
Mouse Actions
Intermediate

Hovering on a Multi-Level Dropdown Menu

πŸš€Scenario Overview

Hovering on a Multi-Level Dropdown Menu

Key Takeaways & Cheat Sheet

  • βœ“Instantiate Actions class to chain complex hover sequences
  • βœ“Wait explicitly for secondary and tertiary submenu visibility after hovers
  • βœ“Use moveToElement() sequentially on each intermediate navigation link
  • βœ“Add short build().perform() execution boundaries for each hover step

Short Direct Answer

Multi-level dropdown menus require sequential hover actions to reveal nested links. To automate this, hover over the parent element using the `Actions` class, wait explicitly for the next submenu to be visible, hover over the submenu element, and repeat this pattern before clicking your target element.

⚠️ Senior Warning (Red Flag)

Do not chain all hovers into a single continuous moveToElement actions block without waits. The UI needs time to animate submenus; clicking immediately will result in NoSuchElementException.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Splitting actions.perform() operations and adding intermediate explicit visibility waits is key to stabilizing multi-level navigation selectors.

SeleniumAutomation.java
Selenium 4 + Java
import org.openqa.selenium.interactions.Actions;

Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));

// βœ… Step 1: Hover over parent menu
WebElement mainCategory = driver.findElement(By.id("nav-electronics"));
actions.moveToElement(mainCategory).perform();

// βœ… Step 2: Wait for submenu and hover over secondary link
WebElement subCategory = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.linkText("Computers"))
);
actions.moveToElement(subCategory).perform();

// βœ… Step 3: Wait for tertiary link and click it
WebElement targetLink = wait.until(
    ExpectedConditions.elementToBeClickable(By.linkText("Laptops"))
);
targetLink.click();