If you have not familiarized with different types of methods of actions class, I will recommend you to look at a glance. You will get a list of all methods under actions class in a very simple way.
Now, let’s come to the main topic. In this tutorial, we will learn how to click at current location and WebElement using actions class methods in Selenium WebDriver.
How to Click at Current Location in Selenium?
To click on the current location, we will use click() method of actions class. The click() method is used to the left-clicking of the mouse at its current location. Let’s automate a scenario related to it.
Scenario to Automate:
In this scenario, we will assume that the Gmail element on Google home page is a current location.
1. Launch the Firefox web browser.
2. Open a URL of the Google home page.
3. Locate the element “Gmail” on the home page using By.xpath.
4. Find the location and coordinate (x, y) of Gmail WebElement.
5. Move the mouse cursor from its initial position to the current location.
6. Now, click on the current location (i.e on Gmail element).
7. Close the web browser.
Now, let’s see the following source code related to the above scenario. Follow all the steps in the below source code.
Program source code 1:
Explanations of Program source code:
1. In the above source code, the statement gmail.getLocation().getX(); has been used to find the location and x coordinate of the location of Gmail WebElement.
2. getY() method with a combination of getLocation() has been used to find y coordinate of the location of Gmail WebElement. The coordinate (x, y) of the Gmail element is nothing but Offset that is the location of Gmail.
3. moveByOffset() method is used to move the mouse cursor from point (0, 0) to point of Gmail element. Note that the initial position of the mouse cursor is (0, 0).
4. After calling moveByOffset() method, we passed the Offset as parameters to the method. +1 in both parameters is added because the initial position is starting from the coordinate (0, 0). That’s why we will have to add +1 to get the exact location of WebElement.
5. Now, click() method of actions class is called to click on the element.
6. actions.build().perform(); is used to perform the chain of actions and clicking.
How to Click on WebElement in Selenium WebDriver?
In the previous section, we have learned to click on WebElement by calculating the offset of it. This technique is not useful every time, especially, when the WebElement has its own attribute such as ID or name.
In this case, we can use another overloaded version of click() method to click directly on any WebElement. The syntax of overloaded click() method to click on web element is as follows:
public Actions click(WebElement onElement)
This method takes an input parameter as an instance of WebElement on which the click action has to be performed.
Now, let us modify the following previous code of above scenario to use click(WebElement element) method. In this coding, we will not use moveByOffset() method.
Program source code 2:
As you can see in the previous coding that the moveByOffset() method has been replaced with click(WebElement onElement) method and the complex coordinate geometry has been removed from the coding. So, if you are a tester, tell your developers to provide attribute for the WebElement. The task will be easy.
Hope that this tutorial has covered almost all important points related to how to click at current location and WebElement in Selenium using Actions class. I hope that you will have understood and learned to click on WebElement using click() method of actions class.
In the next tutorial, we will learn how to double-click at current location and WebElement in Selenium WebDriver.
Thanks for reading!