Mouse hover in Selenium

Mouseover in Selenium WebDriver

Last updated on

Hello friends! in this post, we will learn to automate the mouseover over an element using Selenium Webdriver with Java. For performing the mouse hover over an element in Selenium, we make use of the Actions class. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including mouseover, right-click, double click, drag, and drop, etc.

Code snippet to mouseover

Actions action = new Actions(driver);
WebElement element = driver.findElement("elementId"));

Here, we are instantiating an object of Actions class. After that, we pass the WebElement to be mouse hovered as a parameter to the moveToElement() method present in the Actions class. Then, we will call the perform() method to perform the generated action.

Sample code to mouse hover over an element

For the demonstration of the mouseover action, we will be launching a Sample Site for Selenium Learning. Then we will mouse hover over the ‘Submit’ button. This will result in a tooltip generation, asserting that the mouseover action is successfully performed.

package seleniumTutorials;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class MouseOver {
	public static void main(String[] args) throws InterruptedException{
		WebDriver driver = new FirefoxDriver();
		//Launching sample website
		//Mouseover on submit button
		Actions action = new Actions(driver);
		WebElement btn = driver.findElement("idOfButton"));
		//Thread.sleep just for user to notice the event
		//Closing the driver instance

Please write to us if you have any queries and share your views. Also please share this post on social media and don’t forget to check our complete selenium tutorial here.

Selenium Step-by-Step Tutorial

3 thoughts on “Mouseover in Selenium WebDriver”

  1. Thanks, it’s really good, but it would be much better if you can TestNG, to get rid of the main method, and use the annotations !!

    Good Luck :)

  2. Hello Kuldeep,
    the awesome explanation and hint provided by you regarding automation. I am very impressed. i have facing some issue in my automation (selenium 4 + javascript (node js)). because too less help the community and mostly not got the result. i would request can you create one series or video or documents for this selenium + node js from the initial level.


Leave a Comment