Perform Mouse Hover using WebDriver

Sometimes we need to perform actions like mouse hover, double-click on web elements. There are menus on web application that need mouse hover to display its sub menus.
So don’t worry about it, Selenium WebDriver has a solution. It provides Action class to perform mouse, keyboard actions on Web Elements.
The solution that I am going to explain worked fine for me in my script. I hope it will work for your application as well.
Here I am taking an example of, below script will launch spicejet application and perform mouse hover on “Contact Us” menu and then click on its sub menu “Airports”.


Sample Program :-

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class TestSample {

     public static void main(String s[]) {
         WebDriver driver = new FirefoxDriver();

         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

         //Search for 'Contact Us' and store it in a variable
         WebElement contactUsElement = driver.findElement(By.linkText("Contact Us"));

         //Perform mouse hover on 'Contact Us' web element
         Actions action = new Actions(driver);

         //click on 'Airports' option

If you really like the information provided above, please don’t forget to leave the comment. You can also like us on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *