How to perform mouse hover action using Selenium Wedriver


This post is part of the Selenium Webdriver Commands series. We usually come across some scenarios in automation of mouse hover. It's quiet a tricky thing to perform in automation. Thanks to the Actions Class/commands in Selenium Webdriver.  We already have a Selenium command for it.
Below is the code for Mouse hover in Selenium Webdriver:
/* Mouse Hover */

Actions actions = new Actions(driver);

WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("//loc"));

actions.moveToElement(PRODUCT_CATEGORY);

actions.build().perform();

Explanation of above code:
  1. Creating object of Actions class as actions. And passing driver object to constructor of Actions class.
  2. In second line, creating an Object of WebElement and passing Xpath locator to it.
  3. moveToElement() is the method/command to perform mouse hover.
  4. actions.build().perform(); - This code is mandatory to finish any actions class command. build() method combines multiple actions into one single action and perform() executes those multiple actions as one action.
Below are some of the advanced Selenium Webdriver real time implementations which are quite important with respect to interviews as well (click on the below points to open posts):

Comments

Popular posts from this blog

List of Selenium Demo Websites for Practice

How to learn Selenium Webdriver on your own online