How to perform mouseover function in Selenium WebDriver using Java?

SeleniumAutomation TestingTesting Tools

A mouse hover is done on an element to fire an event on that element. If we hover on the menus of a webpage the submenus appears. Thus this event gets triggered on hovering on an element.

It is evident from the above image that on hovering over the Packages menu the color of the text changed along with the tooltip display. Selenium has the Actions class that contains multiple APIs for mouse cursor movement.

The moveToElement() method is used to perform mouse movement. We have to import org.openqa.selenium.interactions.Actions for Action class. Along with moveToElement() we have to use the perform() method to make the mouse movement.


Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.interactions.Actions;
public class MouseHover{
   public static void main(String[] args) {
      System.setProperty("", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "";
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // identify element
      WebElement l=driver.findElement(By.xpath("//span[text()='Jobs']"));
      // Actions class with moveToElement()
      Actions a = new Actions(driver);
      System.out.println("Tooltip: "+ l.getText());


Updated on 28-Aug-2020 06:20:57