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”));
actions.moveToElement(Menu);WebElement subMenu = driver.findElement(By.linkText(“subMenuLinkText”));
Action action = actions.moveToElement(subMenu).build();
action.perform();

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.chrome.ChromeDriver;
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”;

@Test
public void hover() throws Exception {

// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

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

// this opens the url
driver.get(“https://www.boschtools.com/”);
//Thread.sleep(6000);

popup();

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

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

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

}


@AfterTest
public void tearDown() {

driver.close();
}

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