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

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:

  1. Simulating User Interaction: Mouse hover actions are a frequent method for simulating users’ interactions with the various web elements in the GUI interface. It is said to be the crucial testing method to check whether the program runs as intended.
  2. Checking Responsiveness: Software responsiveness can be checked and tested with mouse hover actions. Responsiveness here, which bothers the audience is the change in the appearance and display of the web elements or the contents present on the web when hovered over.
  3. User Interface Consistency: Consistency in GUI testing with the user interface is also essential, as any inconsistency, such as the appearance or response of the same hovered element again and again, creates much confusion and frustration among the users while working with it.
  4. Bug Detection: Bugs like broken or unauthorized links, any missing tooltip, unresponsive buttons, or other unexpected behavior can be identified, tested, rectified, or reported by the testers or developers with the help of GUI testing or mouse hover actions, leading to the early development process.
  5. Compatibility Testing: Compatibility testing of various browsers or platforms is also done with mouse hover actions. This deals with performance issues that occurred due to different browsers’ performance dependencies, which later got uncovered and handled.

Actions Class in Selenium

Overview of ActionChains Class

The ‘ActionChains’ class is a part of the Selenium WebDriver library. It chains a series of actions that are used to perform various complex and advanced interactions, such as any mouse or keyboard actions, with the webpage or an application. These actions include mouse hover, click, double-click, context-click, drag and drop and key presses, which are briefly discussed below:

  • Mouse hover: In this, the mouse pointer simply hovers over the web elements on the webpage or an application.
  • Click: This action starts or simulates a mouse click over the web elements, such as checkboxes, text areas, links, buttons, radio buttons, etc.
  • Double-click: Similar to click, this action just simulates a double-click over any of the web elements.
  • Context-click: This action helps in simulating a right-click over a web element.
  • Drag and drop: As the name says, this action helps to drag and drop a particular web element from one place to another place on the webpage or an application.
  • Key presses: This action simulates any keyboard actions, such as text typing or pressing any keys on the keyboard.

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:

  1. move_to_element(to_element): This method allows the mouse pointer to move over to a specified element.
  2. move_to_element_with_offset(to_element, xoffset, yoffset): This method helps in moving the mouse pointer to the specified element containing an optional offset, but from the top-left corner.
  3. click(on_element=None): In this, the method helps the mouse pointer to click either on the current mouse position or on the specified element but, only if the element is provided.
  4. context_click(on_element=None): In this too, the method helps the mouse pointer to simulate a double-click either on the current mouse position or on any specified element, but only if the element is provided properly.
  5. double_click(on_element=None): Similarly, here the method allows the mouse pointer to generate or simulate a double-click, either on the current mouse pointer position or on any specified element, given the element is provided properly.
  6. drag_and_drop(source, target): Here, the method helps to drag an element from the source to the target element.
  7. key_down(value, element=None): This method simulates a key press and holds it down. The ‘value’ parameter specifies the key to be pressed, and the optional ‘element’ parameter specifies the element to send the key event to.
  8. key_up(value, element=None): This method helps in releasing a key that was previously held down. The ‘value’ parameter specifies the key to be released, and the optional ‘element’ parameter specifies the element to send the key event to.
  9. send_keys(*keys_to_send): This method sends a sequence of key presses and releases. One can also provide multiple sequences of keys as arguments.
  10. pause(seconds): This method adds a pause (in seconds) to the action sequence, causing a delay between actions.
  11. reset_actions(): This method clears all the actions that have been queued, allowing you to start building a new sequence.
  12. perform(): This method executes the actions that have been queued in the action sequence.

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 “Beginner for Beginner” 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 Beginner {
    @Test
    public void w3wiki() {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3wiki.net/");
        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 “Beginner for Beginner” page.

Java
public class Beginner {
    @Test
    public void w3wiki() {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3wiki.net/");
        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

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?

  1. Actions action=new Actions(driver);
  2. moveByOffset(int xOffSet, int yOffSet)
  3. moveToElement(Web Element);

How to click on location in Selenium?

Locate a particular element and perform the click operation using the click() command.

How do I change the position of my cursor?

  • Setting the cursor position.
  • Pushed the mouse button down.
  • Dragged the cursor across content.
  • Then let the mouse button move up again.


Contact Us