Monday, January 25, 2021

Mouse Hover in Selenium using Java

What is Mouse Hover?

 Mouse hover is a function when we keep mouse cursor on a specific area like URL link or drop-down menu.

 Example:

  1. If I mouse hover on a URL link, it's green color can be changed to other
  2. If I mouse hover on a drop-down menu, it may open sub-menu.

Mouse Hover action in selenium JAVA

//1. Instantiate Action Class
Actions actions = new Actions(driverBrowser);

 

//2. Retrieve WebElement 'Music' to perform mouse hover
WebElement menuOption = driverBrowser.findElement(By.xpath(".//div[contains(text(),'Music')]"));

 

//3. Mouse hover menuOption 'Music'
actions.moveToElement(menuOption).perform();
System.out.println("Done Mouse hover on 'Music' from Menu");