How to handle mouse hover events using Selenium WebDriver

In this article, we will learn about how to handle the mouse hover events using selenium webdriver.

  • Firstly, we have to hover mouse over the menu item. This displays the dropdown list/sub menu items.
  • Next, we have to select the sub elements from the list and have to click on one of the sub element.
  • Click on the sub element.
  • Verify whether selenium webdriver script has clicked the correct link or not.

Menu item Sub menu item Click

Actions Syntax:

Actions actions = new Actions(driver);
WebElement Menu = driver.findElement(By.linkText(“MainMenuLText”));
WebElement subMenu = driver.findElement(By.linkText(“subMenuLinkText”));
Action action = actions.moveToElement(subMenu).build();

Sample Code:

In this example, boschtools website has taken to explain how mouse hover works and how to handle it using selenium webdriver.

package com;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;public class MouseHover {
static WebDriver driver;
String word = “selenium”;
String expectedTitle = “Cordless Combo Kits | Bosch Power Tools”;

public void hover() throws Exception {

// chrome standalone server
driver = new ChromeDriver();

// implicit wait condition
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// this opens the url


WebElement elem1=driver.findElement(By.linkText(“Products”));
Actions act=new Actions(driver);
Action a1=act.moveToElement(elem1).build();

WebElement elem2=driver.findElement(By.linkText(“Cordless Combo Kits”));;

String actualTitle=driver.getTitle();
// Message will get displayed only when assertion is failed.
Assert.assertTrue(actualTitle.equals(expectedTitle), “Title is not correct. Script failed.”);


public void tearDown() {


// Some unexpected popup may display in this website. Below method will handle it.
public void popup(){
if(driver.findElement(By.linkText(“No, thanks”)).isDisplayed() || driver.findElement(By.tagName(“a”)).getAttribute(“aria-label”).equalsIgnoreCase(“Close Modal Box”)){
// Click “No Thanks” button on popup
driver.findElement(By.linkText(“No, thanks”)).click();
}catch(Exception e){}
// Click Close button
}catch(Exception e){}
}catch(Exception e){}

Tags: mouse hover in selenium, mouse hover events in selenium, how to handle mouse hover events in selenium, actions class usage in selenium, actions in selenium, selenium webdriver, selenium, webdriver, selenium automation testing, screenshot using selenium, selenium training, selenium online training, selenium qa testing, selenium automation tool, selenium testing, selenium tutorials, selenium mouse hover, selenium tutorials, selenium testing selenium qa testing, selenium automation