---
description: Why do you need Find Element/s command? Interaction with a web page requires a user to locate the web element. Find Element command is used to uniquely identify a (one) web element within the web page
title: FindElement by XPath in Selenium
image: https://www.guru99.com/images/1/041318_1101_FindElement1.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

FindElement by XPath in Selenium uniquely identifies a single web element on a page, while FindElements returns a complete list of matching elements. Both commands accept a By locator object covering ID, Name, Class Name, and XPath strategies.

* ✅ **Core Principle:** FindElement returns the first matching WebElement; FindElements returns a list indexed from zero, like an array.
* ⚠️ **Exception Handling:** FindElement throws NoSuchElementException when no match exists, whereas FindElements returns an empty list instead of failing.
* 🧭 **Locator Strategies:** Choose among ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, and XPath.
* 🧩 **Implementation Focus:** Pass a By object with a unique locator value to target elements reliably in WebDriver scripts.
* 🚀 **Practical Application:** Working Java examples demonstrate clicking a radio button and iterating a list of elements on a demo page.

[ Read More ](javascript:void%280%29;) 

[](https://www.guru99.com/images/find-element-selenium-1.png)

In Selenium WebDriver, **FindElement** and **FindElements** are the core commands used to locate web elements on a page before any action, such as a click or text entry, can be performed on them.

## Why do you need Find Element(s) command?

Interaction with a web page requires a user to locate the web element first. The Find Element command is used to uniquely identify one web element within the web page, whereas the Find Elements command is used to identify a list of web elements. An element can be identified using multiple [locator strategies](https://www.guru99.com/locators-in-selenium-ide.html) such as ID, Name, Class Name, Link Text, Partial Link Text, Tag Name and [XPath](https://www.guru99.com/xpath-selenium.html).

## FindElement command syntax

Below is the syntax of the FindElement command in Selenium WebDriver:

`WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));`

The FindElement command takes the By object as the parameter and returns a single object of type WebElement. The By object, in turn, can be used with various locator strategies such as ID, Name, Class Name, XPath, etc.

Locator Strategy can be any of the following values:

* ID
* Name
* Class Name
* Tag Name
* Link Text
* Partial Link Text
* XPath

Locator Value is the unique value using which a web element can be identified. It is the responsibility of developers and testers to make sure that web elements are uniquely identifiable using certain properties such as ID or Name.

**Example:**

`WebElement loginLink = driver.findElement(By.linkText("Login"));`

## Example: Find Element in Selenium

The following demo application is used to show the FindElement command in action:

<https://demo.guru99.com/test/ajax.html>

### Scenario:

**Step 1:** Open the AUT

**Step 2:** Find and click the radio button, then click the Check button

package com.sample.stepdefinitions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class NameDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "D:\\3rdparty\\chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get("https://demo.guru99.com/test/ajax.html");

// Find the radio button for “No” using its ID and click on it
driver.findElement(By.id("no")).click();

//Click on Check Button
driver.findElement(By.id("buttoncheck")).click();

}

}

## FindElements command syntax

When you need every element that matches a locator rather than just the first one, use FindElements. Below is its syntax:

`List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));`

### RELATED ARTICLES

* [How to Download and Install Selenium IDE for Firefox & Chrome ](https://www.guru99.com/install-selenuim-ide.html "How to Download and Install Selenium IDE for Firefox & Chrome")
* [Link Text and Partial Link Text in Selenium ](https://www.guru99.com/locate-by-link-text-partial-link-text.html "Link Text and Partial Link Text in Selenium")
* [Live Selenium Webdriver Testing Project ](https://www.guru99.com/live-ecommerce-project.html "Live Selenium Webdriver Testing Project")
* [Top 50 TestNG Interview Questions and Answers (2026) ](https://www.guru99.com/testng-interview-questions.html "Top 50 TestNG Interview Questions and Answers (2026)")

The FindElements command takes the By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value.

**Example:**

`List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));`

### RELATED ARTICLES

* [How to Download and Install Selenium IDE for Firefox & Chrome ](https://www.guru99.com/install-selenuim-ide.html "How to Download and Install Selenium IDE for Firefox & Chrome")
* [Link Text and Partial Link Text in Selenium ](https://www.guru99.com/locate-by-link-text-partial-link-text.html "Link Text and Partial Link Text in Selenium")
* [Live Selenium Webdriver Testing Project ](https://www.guru99.com/live-ecommerce-project.html "Live Selenium Webdriver Testing Project")
* [Top 50 TestNG Interview Questions and Answers (2026) ](https://www.guru99.com/testng-interview-questions.html "Top 50 TestNG Interview Questions and Answers (2026)")

## Example: Find Elements in Selenium

The same demo application is used to show how FindElements iterates over a list of matching elements.

### Scenario:

**Step 1:** Open the URL for Application Under Test

**Step 2:** Find the text of the radio buttons and print it onto the output console

package com.sample.stepdefinitions;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class NameDemo {

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://demo.guru99.com/test/ajax.html");
    List<WebElement> elements = driver.findElements(By.name("name"));
    System.out.println("Number of elements:" +elements.size());

    for (int i=0; i<elements.size();i++){
      System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
    }
  }
}

## Find Element vs Find Elements

Below are the major differences between the Find Element and Find Elements commands:

[](https://www.guru99.com/images/1/041318%5F1101%5FFindElement1.png)

| Find Element                                                                                       | Find Elements                                                                    |
| -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| Returns the first web element if there are multiple web elements found with the same locator       | Returns a list of web elements                                                   |
| Throws the exception NoSuchElementException if there are no elements matching the locator strategy | Returns an empty list if there are no web elements matching the locator strategy |
| Find Element by XPath will only find one web element                                               | It will find a collection of elements that match the locator strategy            |
| Not Applicable                                                                                     | Each web element is indexed with a number starting from 0, just like an array    |

## FAQs

⏱️ How do you avoid NoSuchElementException when an element loads slowly?

Use Selenium [implicit or explicit waits](https://www.guru99.com/implicit-explicit-waits-selenium.html) so the driver polls the page until the element appears. Explicit waits with WebDriverWait and expected conditions are the most reliable approach for dynamic, AJAX-driven pages.

🧭 Which locator strategy is the fastest in Selenium?

ID is the fastest and most reliable locator because browsers index it directly. CSS selectors are generally faster than XPath, so reserve XPath for cases where no unique ID, Name, or class is available.

🤖 Can AI tools generate Selenium locators automatically?

Yes. AI-powered testing assistants can analyze the DOM and suggest stable XPath or CSS locators automatically, reducing manual inspection time. Review generated locators for uniqueness before adding them to production test suites.

🛠️ What are AI self-healing locators in Selenium testing?

Self-healing frameworks use AI to detect when a locator breaks after a UI change and automatically substitute the closest matching element. This reduces test maintenance caused by frequently changing element attributes.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/find-element-selenium-1.png","url":"https://www.guru99.com/images/find-element-selenium-1.png","width":"512","height":"197","caption":"find element selenium","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/find-element-selenium.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/selenium","name":"Selenium"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/find-element-selenium.html","name":"FindElement by XPath in Selenium"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/find-element-selenium.html#webpage","url":"https://www.guru99.com/find-element-selenium.html","name":"FindElement by XPath in Selenium","dateModified":"2026-06-11T18:31:59+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/find-element-selenium-1.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/find-element-selenium.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta","url":"https://www.guru99.com/author/admin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","url":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","caption":"Krishna Rungta","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"Article","headline":"FindElement by XPath in Selenium","description":"Why do you need Find Element/s command? Interaction with a web page requires a user to locate the web element. Find Element command is used to uniquely identify a (one) web element within the web page","keywords":"selenium","author":{"@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta"},"copyrightYear":"2026","name":"FindElement by XPath in Selenium","articleSection":"Selenium","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How do you avoid NoSuchElementException when an element loads slowly?","acceptedAnswer":{"@type":"Answer","text":"Use Selenium implicit or explicit waits so the driver polls the page until the element appears. Explicit waits with WebDriverWait and expected conditions are the most reliable approach for dynamic, AJAX-driven pages."}},{"@type":"Question","name":"Which locator strategy is the fastest in Selenium?","acceptedAnswer":{"@type":"Answer","text":"ID is the fastest and most reliable locator because browsers index it directly. CSS selectors are generally faster than XPath, so reserve XPath for cases where no unique ID, Name, or class is available."}},{"@type":"Question","name":"Can AI tools generate Selenium locators automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI-powered testing assistants can analyze the DOM and suggest stable XPath or CSS locators automatically, reducing manual inspection time. Review generated locators for uniqueness before adding them to production test suites."}},{"@type":"Question","name":"What are AI self-healing locators in Selenium testing?","acceptedAnswer":{"@type":"Answer","text":"Self-healing frameworks use AI to detect when a locator breaks after a UI change and automatically substitute the closest matching element. This reduces test maintenance caused by frequently changing element attributes."}}]}],"@id":"https://www.guru99.com/find-element-selenium.html#schema-1112568","isPartOf":{"@id":"https://www.guru99.com/find-element-selenium.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"image":{"@id":"https://www.guru99.com/images/find-element-selenium-1.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/find-element-selenium.html#webpage"}}]}
```
