Find Element by XPath in Selenium

In this tutorial, we will learn about how to find an element by XPath in Selenium with the help of examples.

We will also understand instantiating a WebDriver object to launch the web browser and work with the most frequently used methods like get(), findElement(), sendKeys(), click(), close() of the WebDriver class.

We have studied that XPath, which stands for XML Path Language, is a query language that allows us to navigate and select nodes in an XML document.

Selenium, which is a popular tool for automating web browsers, supports XPath to find the location of any web element on a web page using XML path expression. All the major web browsers, such as Chrome, Firefox, etc. supports XML Path language.

Let’s learn to find elements by XPath on the web page. We assume that you have learned about get commands, findElement commands, and different types of locators in Selenium.

How to Find Element by XPath in Selenium?


A user requires to find the location of a web element to interact with the web page. To locate a web element on the web page, Selenium WebDriver provides two WebElement commands, such as findElement, and findElements.

Basically, findElement() method helps to uniquely identify a web element within the web page. The general syntax to use findElement() method is as follows:

// This statement will identify one web element within the web page.
WebElement elementName = driver.findElement(By.locatorName("Attribute value"));

findElements() method is used to identify the list of web elements within the web page. It has the following general syntax:

// This statement will identify a list of web elements within the web page.
List<WebElement> elementName = driver.findElements(By.locatorName("Attribute value"));

You can observe in both syntaxes that Find Element commands take an input parameter By object and returns an object of type WebElement.


By is an object of By class which can be used with various locator strategies such as ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, and XPATH. An attribute value of a locator is a unique value that helps to locate or identify a web element on the web page.

Therefore, developers and testers have the responsibility to make sure before identifying an attribute value whether it can uniquely identify web elements using certain locators, such as ID or name or not. For example:

WebElement userName = driver.findElement(By.id("username"));

Here, username is an attribute value of locator id whereas, userName is a variable of type WebElement that is used to store a returning object of type WebElement.

By Class in Selenium


By is an abstract class that extends java.lang.Object class. It is used to locate elements within a document. The general syntax to declare By class in Selenium is as follows:

public abstract class By
       extends java.lang.Object

Let’s automate the following scenario based on the get command, WebElement commands, and locators.

Scenario to Automate:

There are the following steps to complete automating the scenario. They are as follows:

1. Launch the Firefox browser by creating a driver object using WebDriver reference variable.
2. Open the specified web application URL “https://www.facebook.com”.
3. Locate email and password elements on the web page.
4. Locate the login element on the web page and click on the login button.
5. Close the browser.


Now follow all the important steps to find elements by XPath in Chrome Browser.

1. Open URL in the Chrome browser.

2. Right-click on the element on which you are trying to find element by XPath and then go to option “inspect”.

3. Now, the Elements panel will be opened right-hand side of the screen. It will show in blue color.

4. Press ctrl+F to enable DOM searching in the panel.

5. Now you can write XPath in the panel to evaluate for that element.

6. If XPath is matched with any elements, they will be highlighted in DOM in yellow color.

If you have any problem finding elements by XPath in Chrome, go to this tutorial: How to find XPath in Chrome Browser

XPath of Element “email”:
XPath of element email

WebElement email = driver.findElement(By.xpath("//input[@id = 'email']"));


Similarly, XPath of Element “Password”:

XPath of element password

WebElement password = driver.findElement(By.xpath("//input[@id = 'pass']"));

XPath of Element “Login button”:

How to find Element by XPath in Selenium

WebElement login = driver.findElement(By.xpath("//input[@id = 'u_0_2']"));

Let’s write the Java code to automate the following scenario. Look at the following Java code to understand better.

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; 

public class FacebookHomePage { 
public static void main(String[ ] args) 
{ 
 // Declare reference variable of type WebDriver. 
    WebDriver driver; // Create a driver object of Firefox using WebDriver reference. 
         driver = new FirefoxDriver(); 

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

 // Declare Facebook home page URL using a variable of type string. 
    String URL = "https://www.facebook.com"; 

 // Call get() method using reference variable driver and pass the URL as a parameter. 
    driver.get(URL); // It returns nothing. 

 // Wait to load the whole web page. 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

 // Find elements by XPath using findElement command. 
    WebElement email = driver.findElement(By.xpath("//input[@id='email']")); 

 // Enter your login email id in box using sendKeys() method. 
    email.sendKeys("xyz@yahoo.com"); 
    WebElement password = driver.findElement(By.xpath("//input[@id='pass']")); 

 // Enter your login password. 
    password.sendKeys("123456"); 
   
    WebElement login = driver.findElement(By.xpath("//input[@type='submit']")); 
    login.click(); 
    System.out.println("Login successfully"); 

 // Now close the browser. 
    driver.close(); 
  } 
}
Output: 
       Login successfully

Explanation:

1. Import the minimum libraries to run selenium script via WebDriver, WebElement, and FirefoxDriver:
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;

2. When the statement WebDriver driver = new FirefoxDriver() will be executed, a Firefox browser will open.

3. When driver.get(URL); will execute, WebDriver will visit the specified web application URL in your current web browser.

4. WebElement findElement(By by) will receive search parameters (XPath) and find the first matching element present on the current web page and store it in a variable email whose type is WebElement.

5. email.sendKeys(); will send your email id in the mailbox. Similarly, will happen for the password.

6. Now click() method will click on the login button.

7. After login successfully, the close() method will close the current window and exit the web browser.


In this tutorial, we have covered all important points related to how to find element by XPath in Selenium with the help of an example and its explanation. Hope that you will have understood the basic concept of finding the location of any element on the web page.
Thanks for reading!!!