Home » selenium-tutorial » Mouse Hover Action in Selenium

Newly Updated Posts

Mouse Hover Action in Selenium

Previous Topic, Mouse Hover Action in Selenium: its basically an action which is performed when user takes the mouse cursor at any of the elements like menu, Tab etc,

it automatically triggers an event and displays multiple sets of other options.

How to perform Mouse Hover Action?

Sometimes it happens that few of the element in web application are not visible in web page,so in that case to perform an action on the particular element.

we have to take the mouse to a particular element and after that the required element gets visible.

So in this tutorial we will discuss the features of Mouse Hover Action in Selenium.

Mouse Hover in Selenium

In selenium webdriver Using Action Class we can move to any of the element visible in web page. The method exposed to take control on mouse hover in selenium is below.

  • moveToElement(Webdriver)
  • moveToElement(target, xOffset, yOffset)

Lets discuss both defined methods in Action class in details.

1) moveToElement(Webdriver): This method can be called in selenium webdriver once we have created the instance variable of Action class, and calling the instance variable followed by (.) ,we can call the method.So lets code.

So to under this consider a scenario.

  • Launch website http://www.airindia.in/ on chrome browser.
  • Maximise your browser.
  • mouse hover to “Manage Your Trip” menu at the top
  • quit the browser
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class FiestTestScriptChrome {
public Boolean Flag =false;
public static void main(String[] args) throws InterruptedException  {
//System property of the Chrome driver 
			  System.setProperty("webdriver.chrome.driver","C:\\Users\\XXXXXXXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");
			  
 // Create oject of chrome driver 
 WebDriver driver =  new ChromeDriver();
 Thread.sleep(3000);
			  
 driver.get("http://www.airindia.in/");
			 
 driver.manage().window().maximize();
 Thread.sleep(3000);
			  					  
			  
WebElement manageMyTrip =driver.findElement(By.id("aMnu1"));
			   
Actions mouseActn=new Actions(driver);			 			mouseActn.moveToElement(manageMyTrip).build().perform();
			
Thread.sleep(3000);
			  
 driver.quit();
			  
	}

}

So let discuss the code in details.

In the above code after opening the website and maximise the browser, we have created the xpath for element “Manage your Trip” by id attribute as WebElement manageMyTrip =driver.findElement(By.id(“aMnu1”));

After that we have instanciated the Actions class and passing the driver as parameter as Actions mouseActn=new Actions(driver);

After creating the object of Actions class, we used the instance variable (mouseActn) and click to (.) and called the method (moveToMethod) along with build().perform().

The reason for calling the build() method and perform method id as below:

  • Build(): This method is called to generate all the actions on a single action.It helps to build the composite method action and perform the action.
  • perform(): This method is used to perform all the actions defined but we initially provide build method get in a single action.

How to Move To Element in selenium using x,y offset?

2) moveToElement(target, xOffset, yOffset): Till now we have leaned how to mouse hover in selenium using moveToElement() method by passing Webelement , but sometimes using this method we hover mouse at the mid of the element and trigger the event, but in some of the web page we have to hover the mouse action using the x offset and y offset, for ex in Slidder we need to mouse hover till we reach to a defined position.

So in that case we have the to provide the location x offset and y offset as a parameter in oveToElement(target, xOffset, yOffset) method. WE have to perform the action the same way as we did earlier, just need to pass extra parameters to the method as below:

Consider xoffset is 40 and y offset is 10, so the code will as

Actions mouseActn=new Actions(driver);			 			mouseActn.moveToElement(manageMyTrip, 40,10).build().perform()