
Sometimes sub menu of an web element triggers when a user assigns a mouse to the specified web element. In that case, we face difficulty to click on sub menu item. In order to perform mouse hover actions, we need to make the driver move to the parent element that has sub elements and click on the sub element. Sometimes We need to verify tool tip on an element. Lets see below how we are going to achieve this easily.
Mouse Hover Action in Selenium:
// Create object of Action class
Actions action = new Actions(driver);
// Find element using locator and store into WebElement
WebElement element = driver.findElement(By.id("elementId"));
// Perform moveToElement operation using action (object) on element.
action.moveToElement(element).perform();
Mouse Hover and Click on Sub menu using Action Class:
- In order to handle mouse hover action on any web element and click on any sub menu of it we use Actions class.
- After moving the mouse cursor on desired element we need to use build() method, that is used to compile all the listed actions into a single step to perform any task.
// Find element using locator and store into WebElement
WebElement elementToHover = driver.findElement(By.id("elementToHover "));
// Find element using locator and store into WebElement
WebElement elementToClick = driver.findElement(By.id("elementToClick "));
// Create object of Action class
Actions action = new Actions(driver);
// Perform moveToElement operation using action (object) on element.
action.moveToElement(elementToHover).click(elementToClick).build().perform();
Manual Actions:
- Launch the web browser and open the application
- Find the required element
- Place cursor on it and click on any sub menu
- Verify Sub menu text (Tool Tip)
- Close the browser
Selenium WebDriver Steps
package automationTestingLab;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class MouseHover {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://qaclickacademy.com/practice.php");
//Find the web element which shows sub menu on hovering
WebElement mouseHoverElement = driver.findElement(By.id("mousehover"));
//Sub menu element
WebElement subMenu = driver.findElement(By.xpath("//a[contains(text(),'Top')]"));
//Scroll to the element
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView();", mouseHoverElement); //Scroll to the element
// Create object of Action class
Actions action = new Actions(driver);
//Verify sub menu text
action.moveToElement(mouseHoverElement).perform();
String subMenuText = subMenu.getText();
if(subMenuText.equalsIgnoreCase("top")){
System.out.println("Test Case iS Passed");
}
// Perform moveToElement operation using action (object) on element.
action.moveToElement(mouseHoverElement).click(subMenu).build().perform();
Thread.sleep(3000);
driver.close();
}
}