How to perform Mouse Hover in Selenium WebDriver

There might be some cases when you want to perform Mouse Hover in Selenium WebDriver, which means moving your mouse over an element before doing some operation. It could be for checking tooltips or enabling some context menu or any other type of reason.

In Selenium automation, we can utilize the Actions class to get this kind of operation. Actions class provides methods for an operation like Mouse Hover, Left click, Right click, etc. Mostly it provides a method to replicate any operation which can be performed by a real keyboard/mouse.

For understanding how to use it, let’s consider that we have a Menu that displays submenus when we move the mouse over it (like a mega menu).

In order to click an item in the submenu, first we need to hover the mouse pointer on the menu element right?

In the below example, notice how the method MoveToElement is used to replicate the mouse hover on MenuElement and then on the submenu item with XPath “Xpath-Of-Menu-Item-Element”)

Example code for Mouse Hover in Selenium WebDriver
Actions Actions = New Actions(Driver);
WebElement MenuElement = Driver.FindElement(By.Id("Menu-Element-Id"));
Actions.MoveToElement(MenuElement).MoveToElement(Driver.FindElement(By.Xpath("Xpath-Of-Menu-Item-Element"))).Click();
You may also read
Java | Selenium | Testing Tools

How to add APACHE POI in Selenium Project – Apache POI

APACHE POI is the most used library for working with Excel workbooks in Selenium Automation projects. There are two ways you can add these jars to your automation project. One way is through the POM.xml in your maven project and the other way is by downloading the jars directly from Apache website and adding them in build path of your automation project.

jenkins

How to integrate Selenium with Jenkins – Step-by-step Example

FacebookTweetPinShares0Email Given the popularity of cloud technology, knowing How to integrate Selenium with Jenkins pipeline has become very important. Jenkins is the most famous tool for CI-CD. With the rapidly evolving software development industry, testing is also adopting the “Shift Left”…

Similar Posts

Leave a Reply

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