Click on WebElement and Current Location in Selenium

In the previous tutorial, we understood the basics of actions class and action interface in Selenium WebDriver. We also learned different types of methods available under the action interface and actions class.

If you have not been familiarized with the 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 the 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.

[adinserter block=”5″]

Let’s see the following program code related to the above scenario. Follow all the steps in the below code.

Program code 1:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 

public class ClickAtCurrentLocation { 
public static void main(String[] args) 
{ 
 // Create a driver object of Firefox browser. 
    WebDriver driver = new FirefoxDriver(); 

 // Maximize the browser. 
    driver.manage().window().maximize(); 

 // Create a variable URL to store the URL of Google home page. 
 // Since the return type of URL is String, we will declare URL as String type. 
    String URL = "https://www.google.com"; 

 // Call get() method of WebDriver and pass URL as a parameter. 
    driver.get(URL); 

 // Wait for some time to load. 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

 // Locate the element “Gmail” by By.xpath. 
    WebElement Gmail = driver.findElement(By.xpath(“//a[text()=’Gmail’]”)); 

 // Create an object of Actions class and pass reference variable driver as a parameter to its constructor. 
    Actions actions = new Actions(driver); 

 // Get the location and Coordinate (x, y) of WebElement “gmail”. Call getLocation(), getX(), and getY() methods to find the location and coordinate. 
    int getX = gmail.getLocation().getX(); 
    System.out.println(“X coordinate: ” +getX); 
    int getY = gmail.getLocation().getY(); 
    System.out.println(“Y coordinate: ” +getY); 

 // Call moveByOffset() method of Actions class to move the mouse cursor from initial position to given Offset. 
 // Pass the coordinates of x and y as parameters to moveByOffset() method. 
    actions.moveByOffset(getX+1, getY+1).click(); 
    actions.build().perform(); 
  
    System.out.println(“Clicked Successfully on Gmail”); 
    driver.close(); 
  } 
}
Output: 
       X coordinate: 1285 
       Y coordinate: 19 
       Clicked Successfully on Gmail

Explanation:

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.
[adinserter block=”2″]

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 its offset. This technique is not used 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 code 2:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 

public class ClickOnWebElementDirectly { 
public static void main(String[] args) 
{ 
 // Create a driver object of Firefox browser. 
    WebDriver driver = new FirefoxDriver(); 

 // Maximize the browser. 
    driver.manage().window().maximize(); 

 // Create a variable URL to store the URL of Google home page. 
 // Since the return type of URL is String, we will declare URL as String type. 
    String URL = "https://www.google.com"; 

 // Call get() method of WebDriver and pass URL as a parameter. 
    driver.get(URL); 

 // Wait for some time to load. 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

 // Locate the element “Gmail” by By.xpath. 
    WebElement gmail = driver.findElement(By.xpath(“//a[text()='Gmail']”)); 

 // Create an object of Actions class and pass reference variable driver as a parameter to its constructor. 
    Actions actions = new Actions(driver); 
    actions.click(gmail); 
    actions.build().perform(); 
    
    System.out.println(“Clicked Successfully on Gmail”); 
    driver.close(); 
  } 
}
Output: 
      Clicked Successfully on Gmail

As you can see in this 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 attributes 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!!!

⇐ PrevNext ⇒