MouseHover action in selenium WebDriver

Mousehover action in selenium WebDriver –

In the Actions class, the user, can perform one action, by simply calling it into the driver instance, followed by the method perform().

MouseHover using Actions Class in Selenium WebDriver

Let us see about mousehover action in selenium. If the menu bar or anywhere on the page, the menu has dropdown, and the dropdown options appear only when you hover the mouse on that menu.

So, in this case, we can use mouse hover action of the Actions class in selenium

Let us see, hot this can be achieved.

In my blog (URL: http://automationtalks.com/), suppose you want to select the sub-menu (Selenium WebDriver) of Menu: Selenium.

See below screen.
MouseHover using Actions Class in Selenium WebDriver
To Achieve this, follow below mentioned steps.
  1. Open the browser.
2.    Navigate to the given URL (http://automationtalks.com/).
  1. Mouse Hover on “Selenium Tutorial” tab present in the top navigational bar.
  2. Select the Menu which you want to click on (let us consider here as – “Selenium WebDriver”.
  3. Click on the “Selenium WebDriver”.
  4. Close the browser.
First define actions class –

                        Actions act = new Actions(driver);

Then, mousehover using actions class, syntax below –

act.moveToElement(driver.findElement(“webelement_Menu_location”)).build().perform();

Now, the submenu should be visible, Then click on the submenu –

driver.findElement(“WebElement_submenu”).click();

Below is the example how can be mouse hover achieved in selenium WebDriver.

package demoPackage1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class ActionsClass {
  
  @Test
  public void TestCase() throws InterruptedException{
    
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://automationtalks.com/");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    Actions act = new Actions(driver);

    //Define actions class & move to webelement  (Hove mouse to webelement)
    act.moveToElement(driver.findElement(By.xpath("//span[contains(.,'Selenium Tutorial')]"))).build().perform();
    
    //After mouse hover, element form dropdown is appeared, then click on it.
    driver.findElement(By.xpath("//a[contains(.,'Selenium WebDriver')]")).click();
    
    Thread.sleep(3000);
    
    
    driver.close();
    driver.quit();
    
    

    
  }

}

Hope This Helps !!!!

4 Comments

  1. abc

    Reply

    Hello Prakash,
    Am able to do mouse hover on Selenium Tutorial and click on Selenium Webdriver as normallly but am not able to inspect the mouse hover on Selenium Tutorial and not able to inspect the Selenium Webdriver too.
    Even i tried doing with other websites not able to do am not able to choose when am inspecting the element.
    What is the solution for this?

    • Prakash Narkhede

      Reply

      What you are saying is, you are not able to inspect element itself?
      Did you tried using F12? or in any other browser?

  2. abc

    Reply

    no no sorry i didnt asked u clearly…
    i am able to inspect Selenium tutorial…but how to inspect when we mouse hover on Selenium tutorial and want to get other link from that.

    • Prakash Narkhede

      Reply

      FIrst you hove mouse on Selenium Tutorial, right click & go to inspect element –> you will locator for Selenium Tutorial
      Now, hover mouse on Selenium Tutorial, go to its subelement with mouse,rightclick on that subelement & inspect element –> you should get what you are looking for

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories