If you are looking for a working solution of – How to perform mouseover function in Selenium WebDriver using Java? Then, you have come to the right place. In this step-by-step tutorial, we will learn about the Mouse Over Function practical example
By the way, there is also another popular Selenium Interview Question that we have explained here – How to get mouseover text in Selenium WebDriver Java? So, in case if you are interested, you can learn that after this tutorial – How to perform Mouseover in Selenium?
Now, Let’s get started with our Selenium Framework Mastery Series question on performing mouseover function in Selenium WebDriver with Java –
Test Code – To Perform Mouseover Function : Selenium + Java
The most efficient solution to perform mouseover function in Selenium and Java is using the moveToElement(..)
method of Actions
class. Let’s see, How to do mouseover and then we will explain – what each statement is doing in this test code.
// Step 1 - Define the package and import the required APIs / Classes.
package com.logicalduniya.practice.selenium;
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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
// Step 2 - Created a Test Class containing the test cases for mouseover.
public class MouseoverSeleniumTest {
// Created a reference variable of WebDriver interface, to hold the WebDriver object.
private static WebDriver driver;
// The main method will contain all the executable code.
public static void main(String[] args) throws Exception {
// Step 3 - Setup() method with handle the Setup of ChromeDriver, to talk with real Chrome browser.
setup();
System.out.println("---- WebDriver Setup is Completed !!");
// Step 4 - Perform the mouseover function-
mouseOverClickTest();
System.out.println("---- Mouse Over Click Test is Done Successfully !!");
}
public static void setup() {
System.setProperty("webdriver.chrome.driver",
"/Users/logical_duniya_com/Desktop/OtherFiles/ChromeDriver/chromedriver_mac");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("start-maximized"); // open Browser in maximized mode
chromeOptions.addArguments("disable-infobars"); // disabling infobars
chromeOptions.addArguments("--ignore-certificate-errors");
chromeOptions.addArguments("--remote-allow-origins=*");
chromeOptions.setAcceptInsecureCerts(true); // AcceptInsecureCerts
driver = new ChromeDriver(chromeOptions);
}
public static void mouseOverClickTest() throws Exception {
// Open Google homepage
driver.get("https://www.w3schools.com/howto/howto_css_dropdown.asp");
System.out.println("---- Reached on WebPage having Sample Hover Menu !!");
// Locate the Google Search button
WebElement searchButton = driver.findElement(By.className("dropdown2"));
// Perform mouseover (hover) action on the search button
System.out.println("---- Performing Mouse Over on Sample Hover Menu !!");
performMouseover(driver, searchButton);
// Wait for 2 seconds to observe the mouseover effect
Thread.sleep(5000);
// Close the browser
driver.quit();
}
private static void performMouseover(WebDriver driver, WebElement element) {
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();
}
}
Output
---- WebDriver Setup is Completed !!
---- Reached on WebPage having Sample Hover Menu !!
---- Performing Mouse Over on Sample Hover Menu !!
---- Mouse Over Click Test is Done Successfully !!
POM.xml file –
If you are using Maven based Java project, then to execute the above Mouseover Selenium Test code, you would require below dependencies / libraries. Keep the below code in your pom.xml file –
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicalduniya</groupId>
<artifactId>LogicalDuniyaSoftwareTesting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Step-by-Step explanation of Mouseover Selenium Test Code
As you can see, we have explained the mouseover function Selenium Java test code – with help of figures and images. Now, let’s go through the each code segment in above simple solution of Selenium WebDriver script for performing a mouseover :
package com.logicalduniya.practice.selenium;
:
This line specifies the package name in which the class (MouseoverSeleniumTest
) will be kept later. Packages help to organise classes and avoid naming conflicts, so it would be really good if you provide any package name as per your convenience.import
Statements:
These statements import the necessary classes and interfaces from the Selenium library and Java core libraries. These imports allow the usage of Selenium methods and classes in the test code.private static WebDriver driver;
:
This line declares a private static variable driver of type WebDriver. It will hold the WebDriver object that allows interaction with the browser.public static void main(String[] args) throws Exception { … }
:
This is the main method of the class, where the execution of the script starts. It simply handles the setup of theWebDriver
and calls themouseOverClickTest()
method to perform the mouseover function.setup()
: This method sets up theChromeDriver
to communicate with the Chrome browser. It configures theChromeOptions
with specific arguments, such as starting the browser in maximized mode and ignoring certificate errors.mouseOverClickTest()
:
This method contains the test logic to demonstrate the mouseover functionality. It opens a webpage ofw3schools.com
This webpage contains a sample hover menu, locates the element representing the dropdown menu, and performs a mouseover action on it using theperformMouseover()
method. This is created by us, to separate the business logic from themain(..)
method.performMouseover(WebDriver driver, WebElement element)
: This method uses theActions
class from Selenium, to perform the actual mouseover action on the given element (WebElement
). It moves the mouse pointer to the centre of the element and then performs the action using theperform()
method.
private static void performMouseover(WebDriver driver, WebElement element) {
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();
}
- Once the mouseover is executed, the script waits for 5 seconds to observe the effect and then closes the browser using
driver.quit()
. The script shows how to perform and verify a mouseover action using Selenium WebDriver with Java.
Conclusion
The script demonstrates how to use Selenium’s Actions
class to perform a mouseover (hover) action on a web element. In this example, the mouseover is demonstrated on a sample hover menu from the W3Schools website.
By the way, there is also another popular Selenium Interview Question that we have explained here – How to get mouseover text in Selenium WebDriver Java? So, in case if you are interested, you can learn that after this tutorial. Also, you can check our Selenium Framework Mastery Series to learn Selenium and prepare for the interviews.
If you have any question / suggestion, then please mention them in comment section below, I’ll try to answer your query, within 24 hours.
Cheers,
Shubham !!
[…] our previous Selenium Interview Question, we have already learnt How to perform mouseover function in Selenium WebDriver using Java?. Now net’s get started with out Selenium get Tooltip text Java and Selenium WebDriver based […]