Wednesday, July 3, 2024
HomeLanguagesJavaHow to Move Mouse Cursor to a Specific Location using Selenium in...

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 to perform 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 the mouse user actions and the Keyboard actions. The actions provided by this class are performed by an API called Advanced user interaction in selenium webdriver. Some of the important mouse events provided by the Action class are: moving the cursor pointer to an element, double-clicking, right-clicking, and frag and drop.

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 are not able to click the element through the click method in that case we need to click the element by mouse action. It provides two ways for moving 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.

Let’s see an example program:

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

 

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 neveropen() {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.neveropen.co.za/");
        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.

 

It clicked the link and the web page 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 neveropen() {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.neveropen.co.za/");
        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.

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments