i

Selenium Step By Step Guide

Handling Mouse Hover in Selenium

Handling “Mouse Hover” in Selenium:

We usually see some situations where mouse hovering action on a menu tab gives a list of options to select. Handling this kind of action is very easy in Actions class. We can perform "Mouse-hovering" action on https://selflearning.io/login web page and hover the mouse action on the "GOVERNMENT JOB" tab. Let us see how to handle the "mouse hover" in Selenium.

Let us instantiate the Actions class.

        Actions action=new Actions(driver);

Now we need to form the action sequence and perform the composite action by using perform() method of the Actions class.

action.moveToElement(ele2).perform();

 

Let us see the complete code of handling this situation:

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.interactions.Actions;

public class ActionsClass

{

    public static void main( String[] args )

    {

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/");

        WebElement ele2=driver.findElement(By.xpath("/html/body/header/div[4]/div/ul/li[1]/a"));

        Actions action=new Actions(driver);

        action.moveToElement(ele2).perform();

       

    }

}