---Advertisement---

How to Use Find Element and Find Elements by XPath in Selenium WebDriver Great 2025

By Shiva

Updated On:

---Advertisement---
How to Use Find Element and Find Elements by XPath in Selenium WebDriver Great Opportunity-2025

1.Learn how to use FindElement and FindElements by XPath in Selenium WebDriver for efficient web automation.

2.Master XPath selectors to locate elements precisely in Selenium WebDriver scripts.

3.Use FindElement to get a single matching element and FindElements to retrieve multiple elements by XPath.

4.Optimize XPath expressions for faster and more reliable Selenium automation testing.

5.Handle dynamic elements in Selenium WebDriver using flexible XPath techniques.

6.Avoid common XPath mistakes to improve the accuracy of element selection in Selenium.

7.Combine XPath functions like contains(), starts-with(), and text() for better element identification.

8.Debug and validate your XPath queries using browser developer tools before using them in Selenium.

9.Use relative XPath over absolute XPath to make your Selenium scripts more robust.

10.Improve test automation efficiency by choosing the right XPath strategies for Selenium WebDriver.

When automating web applications using Selenium WebDriver, interacting with web elements is crucial. Selenium provides two primary methods to locate elements on a webpage:

  • FindElement: Used to locate a single web element.
  • FindElements: Used to locate multiple elements.

In this guide, we’ll explore these methods, their syntax, use cases, and examples with XPath in Selenium.


Optimize your Selenium scripts by leveraging XPath selectors for robust and flexible web automation.

Understanding absolute and relative XPath improves element identification in Selenium WebDriver.

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

The FindElement command in Selenium WebDriver takes a By object as a parameter and returns a single WebElement. If multiple elements match the criteria, only the first element found is returned.

  • ID
  • Name
  • Class Name
  • Tag Name
  • Link Text
  • Partial Link Text
  • XPath
WebElement loginLink = driver.findElement(By.linkText("Login"));
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) {
        System.setProperty("webdriver.chrome.driver", "D:\\3rdparty\\chrome\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        
        driver.get("http://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();
    }
}

XPath in Selenium WebDriver helps identify dynamic web elements with precision for automated testing.

Use FindElement to locate a single element and FindElements to fetch multiple matching elements in Selenium.

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

The FindElements command returns a list of WebElements that match the given locator strategy. If no elements are found, an empty list is returned.

List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
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("http://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"));
        }
    }
}

FeatureFindElementFindElements
Return TypeReturns a single WebElementReturns a list of WebElements
If Multiple MatchesReturns the first matched elementReturns all matched elements
If No MatchesThrows NoSuchElementExceptionReturns an empty list
Use CaseUsed when you expect a single elementUsed when multiple elements are expected

When using findElement, if the element is not found, Selenium throws a NoSuchElementException. To avoid this, you can:

  • Use try-catch block:
try {
    WebElement element = driver.findElement(By.id("non-existent-id"));
} catch (NoSuchElementException e) {
    System.out.println("Element not found!");
}
  • Use findElements to check if elements exist before interacting with them:
List<WebElement> elements = driver.findElements(By.id("non-existent-id"));
if (elements.isEmpty()) {
    System.out.println("No elements found.");
}

  • FindElement returns a single element and throws an exception if not found.
  • FindElements returns a list of elements and returns an empty list if no match is found.
  • XPath and other locator strategies can be used to locate elements effectively.
  • Handle exceptions properly to avoid script failures.

Leave a Comment

Index