FindElement and FindElements in Selenium
Last Updated :
23 Jul, 2025
In Selenium, FindElement and FindElements are essential methods used for locating web elements on a webpage. These methods allow you to interact with the elements and perform various actions such as clicking, entering text, and retrieving information.
What is FindElement?
FindElement is Used to locate a single web element on the webpage. This method returns the first matching element based on the specified locator strategy. It always returns a single element and in case of multiple elements, it returns the first element. If no element is found, it throws a NoSuchElementException.
Example: WebElement element = driver.findElement(By.id("elementId"));
What is FindElements?
FindElements is used to locate multiple web elements on the webpage. This method returns a list of all matching elements based on the specified locator strategy. It returns a single element in case of one element found and in case of multiple elements, it returns all the elements. If no elements are found, it returns an empty list rather than throwing an exception.
Example: List<WebElement> elements = driver.findElements(By.className("elementClass"));
Difference between FindElement and FindElements in Selenium
Here are the following differences between FindElement and FindElements in Selenium:
Aspect | FindElement | FindElements |
---|
Purpose | FindElement Locate a single web element | FindElements Locate multiple web elements |
Return Type | FindElement returns a WebElement | FindElements return List<WebElement> |
Locator Strategies | FindElement supports ID, name, class name, etc. | FindElements supports ID, name, class name, etc. |
Behavior if Not Found | FindElement throws NoSuchElementException | FindElements returns an empty list |
First Matching Element | It returns the first matching element | It returns all matching elements |
Example | WebElement element = driver.findElement(By.id("elementId")); | List<WebElement> elements = driver.findElements(By.className("elementClass")); |
Common Use Cases | Actions on a single element (click, type) | Actions on multiple elements (iteration, bulk actions) |
FindElement in Selenium command Syntax (with explanation)
Syntax of FindElement
WebElement element = driver.findElement(By.locator("value"));
Explanation of Syntax of FindElement
- WebElement: The type of object that represents a web element on a webpage.
- element: The variable name for the web element you are locating.
- driver: The instance of WebDriver you are using to control the browser.
- findElement: The method used to locate a single web element.
- By.locator("value"): The strategy for finding the element. By is a class providing various locator strategies.
Various Locator Strategies
Various Locator Strategies are:
- By.id("elementId"): Locate by the element's ID attribute.
- By.name("elementName"): Locate by the element's name attribute.
- By.className("elementClass"): Locate by the element's class name.
- By.tagName("tagName"): Locate by the element's tag name.
- By.linkText("linkText"): Locate by the exact text of a link.
- By.partialLinkText("partialText"): Locate by a portion of the link text.
- By.cssSelector("cssSelector"): Locate using a CSS selector.
- By.xpath("xpathExpression"): Locate using an XPath expression.
Example
WebElement loginButton = driver.findElement(By.id("loginButton"));
- WebElement loginButton: This declares a variable loginButton of type WebElement.
- driver.findElement: This calls the findElement method on the driver object.
- By.id("loginButton"): This uses the ID locator strategy to find the element with the ID "loginButton".
Find Elements in Selenium command Syntax
Syntax of FindElements
List<WebElement> elements = driver.findElements(By.locator("value"));
Explanation of Syntax
- List<WebElement>: A list that holds multiple WebElement objects.
- elements: The variable name for the list of web elements you are locating.
- driver: The instance of WebDriver you are using to control the browser.
- findElements: The method used to locate multiple web elements.
- By.locator("value"): The strategy for finding the elements. By is a class providing various locator strategies.
Example
List<WebElement> buttons = driver.findElements(By.className("buttonClass"));
- List<WebElement> buttons: This declares a variable buttons that holds a list of WebElement objects.
- driver.findElements: This calls the findElements method on the driver object.
- By.className("buttonClass"): This uses the class name locator strategy to find all elements with the class name "buttonClass".
Related Articles:
Conclusion
In conclusion, the findElement and findElements methods in Selenium are used to locate web elements on a webpage. findElement is for finding a single element and will throw an exception if no element is found. In contrast, findElements is for finding multiple elements and will return an empty list if no elements match the criteria. Choose findElement when you expect a single match and findElements when you need to interact with multiple elements.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING