Find Element by XPath in Selenium with Example

Find Element by XPath in Selenium | In the previous tutorial, we have studied the basic syntax of XPath, absolute and relative XPath in Selenium.

I assume that you have learned to get commands, findElement commands, and different types of locators in Selenium.

In this tutorial, we will able to learn the following topics:

  • Instantiate a WebDriver object to launch the web browser.
  • Find element by XPath in Selenium.
  • Work with the most frequently used method like get(), findElement(), sendKeys(), click(), close() of the WebDriver class.

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:

WebElement elementName = driver.findElement(By.locatorName("Attribute value")); // Identify one web element within web page.

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

2. List<WebElement> elementName = driver.findElements(By.locatorName("Attribute value")); // Identify list of web elements within web page.

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 get command, WebElement commands, and locators.

Scenario to Automate:

There are the following steps to complete to automate 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 start coding to automate the following scenario. Look at the following source code to understand better.

Program source 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 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("[email protected]"); 
     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 finds 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 exits the web browser.


Hope that this tutorial has covered all important points related to how to find element by XPath in Selenium with example and explanation. I hope that you will have understood this concept nicely.
Thanks for reading!!!Next ⇒ XPath Contains, Text⇐ PrevNext ⇒

Leave a Comment