DEV Community

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

Posted on • Edited on

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

✅ Join us - https://sendfox.com/thetestingacademy

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: https://scrolltest.com/automation/day17

🚀 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);
action.moveToElement(element).click().perform();

🚀 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.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

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

public class MouseHover {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("https://in.ebay.com/");
        driver.manage().window().maximize();

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

        //Mouse hover actions on an element using Actions Class:
        action.moveToElement(element).build().perform();


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


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

        System.out.println(driver.getCurrentUrl());



    }
}


--
Be sure to subscribe for more videos like this!

 TheTestingAcademy

Top comments (0)