How to implement Mouse Hover in Selenium using the Actions Class?

Let’s discuss moving the cursor pointer using the Action class in Selenium Webdriver

On some web pages, we need to move the cursor to check the mouse hover functionality and in some cases, if we cannot click the element through the click method, we need to click the element by mouse action. It provides two ways to move the cursor pointer,

  1. To a specific location.
  2. To a specific Element in the webpage

To work with the Actions class, first, we need to declare the actions class and import it “import org.openqa.selenium.interactions.Actions;”.

Actions action=new Actions(driver);

1. Move the Mouse Cursor to a Specific Location

To move the mouse cursor pointer to a specific location, the Action class provides the method called,

moveByOffset(int xOffSet, int yOffSet)

Here x offset defines the horizontal axes and the Y offset refers to the vertical axes of the web page.

Example:

In this program, we launch the “geeks for geeks” website and click a link using the offset value.

Move the Mouse Cursor to a Specific Location

In this program, we move the cursor pointer to this coordinate and click this link using the Actions class.

Java
public class Geeks {
    @Test
    public void w3wiki() {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3wiki.org/");
        Actions action=new Actions(driver);
        action.moveByOffset(460,540).click().build().perform();
        driver.close();        
    }
}

Output:

In this program, the mouse pointer is moved to the specified location, and perform the click operation on that location.

Perform the click operation on that location

When click the link and then the web page is opened.

2. Move the Mouse Pointer to a Specific Element

To move the mouse pointer to a specific web element the Actions class provides a method called,

moveToElement(Web Element);

Let’s see an example program, in this program, we move the mouse pointer to the specific web element present in the “geeks for geeks” page.

Java
public class Geeks {
    @Test
    public void w3wiki() {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3wiki.org/");
        Actions action=new Actions(driver);
        WebElement link=driver.findElement(By.xpath("//*[@id=\"home-page\"]/div/div[3]/div[2]/div/div[2]/div/ul/li[4]/a/span[1]"));
        action.moveToElement(link).build().perform();
        driver.close();        
    }
}

Output:

This program moves the mouse pointer to the specified web element.

Move the Mouse Pointer to a Specific Element

How to Move Mouse Cursor to a Specific Location using Selenium in Java?

Selenium is an open-source Web Automation tool that supports many user actions in the web browser. For automating a web page the mouse movement is the most important action taken by the users, so to perform the mouse actions the selenium provides a class called Actions. The Action class provides the method for all mouse user and Keyboard actions. Some of the important mouse events offered by the Action class are: moving the cursor pointer to an element, double-clicking, right-clicking, and frag and drop.

Table of Content

  • What is a Mouse Hover Action?
  • Importance of Mouse Hover Actions in GUI Testing
  • Actions Class in Selenium
  • Methods and Properties of the Actions Class
  • How to implement Mouse Hover in Selenium using the Actions Class?
  • Conclusion
  • Frequently Asked Questions on How to Move Mouse Cursor to a Specific Location using Selenium

Similar Reads

What is a Mouse Hover Action?

The mouse pointer hovers over the web elements on the webpage or an application. In computing, a mouse hover is an action that happens when you move the pointer over a specific area of the webpage, with the help of the mouse or digital pen. This action is common in web browsers....

Importance of Mouse Hover Actions in GUI Testing

Mouse hover actions are also essential for GUI (Graphical User Interface) testing as they play a significant role for several reasons, such as:...

Actions Class in Selenium

Overview of ActionChains Class...

Methods and Properties of the Actions Class

The ‘ActionChains’ class in Selenium also provides several methods and properites that help in performing various actions and interactions on a webpage. Some of them are discussed below:...

How to implement Mouse Hover in Selenium using the Actions Class?

Let’s discuss moving the cursor pointer using the Action class in Selenium Webdriver...

Conclusion

In summary, the Selenium Actions class is essential for automating the complex user interactions such as mouse movements and key press on web pages. Key methods include move_to_element, and drag_and_drop, among others, facilitating comprehensive GUI testing. Proper implementation of these actions ensures robust and responsive web applications....

Frequently Asked Questions on How to Move Mouse Cursor to a Specific Location using Selenium

How do I move the cursor to a specific location in Selenium?...

Contact Us