DEV Community

Cover image for ✅ How To Perform the Mouse Hover action using Action Class in Selenium?
Pramod Dutta
Pramod Dutta

Posted on • Updated on

✅ How To Perform the Mouse Hover action using Action Class in Selenium?

✅ Join us -

In this article, We are going to learn How To perform the Mouse Hover action using Action Class in Selenium OR Selenium Webdriver.

You can use the Action class to perform the mouse movement like hover, navigate, moveToElement and Keyboard events like click, clickAndhold and Doubleclick extra.

✅ We will learn handling Keyboard and Mouse Event in Selenium.

Selenium Mouse Hover

🚀 Download Solution:

🚀 What is Action Class in Selenium?

Actions class is used in Selenium for handling keyboard and mouse events.

In Selenium, handling these events includes operations such as drag and drop, clicking on multiple elements with the control key, among others.

These operations are performed using the advanced user interactions API.

It mainly consists of Actions that are needed while performing these operations.

Action class is defined and invoked using the following syntax:

Actions action = new Actions(driver);

🚀 Methods of Action Class

Mouse Actions:

doubleClick(): Performs double click on the element
clickAndHold(): Performs long click on the mouse without releasing it
dragAndDrop(): Drags the element from one point and drops to another
moveToElement(): Shifts the mouse pointer to the center of the element
contextClick(): Performs right-click on the mouse

Keyboard Actions:

sendKeys(): Sends a series of keys to the element
keyUp(): Performs key release
keyDown(): Performs keypress without release

package SeleniumCommands;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.time.Duration;
import java.util.List;

public class MouseHover {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();

        Actions action = new Actions(driver);
        WebElement element = driver.findElement(By.linkText("Electronics"));

        //Mouse hover actions on an element using Actions Class:

               WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Smart Watches")));

        WebElement element2 = driver.findElement(By.linkText("Smart Watches"));
        //Mouse hover actions on a sub-element using Actions Class:;



Be sure to subscribe for more videos like this!


Top comments (0)