SlideShare a Scribd company logo
findElement and
findElements in Selenium
U S E C A S E S W I T H E X A M P L E S
findElement and findElements in
Selenium: Use Cases with Examples
When we want to interact with any web page, we use locators to
identify the web elements and interact with them using locators.
We use HTML elements on the web page while automating any
web based application using Selenium WebDriver.
Selenium WebDriver has two methods for identifying the web
elements, they are findElement and findElements.
1. findElement: This is used to uniquely identify any web
element within the web page.
2. findElements: This is used to uniquely identify the list of
web elements within the web page.
These methods are very important for locating web elements on a
web page, but they serve different purposes and have distinct use
cases.
Let us see what exactly the both methods are:
1. Find Element():
● findElement() is a method in the WebDriver Interface in
Selenium.
● It is used to locate the first element that matches the
specified locator strategy and conditions.
● If no matching element is found, it throws a NoSuch Element
Exception.
● The result of findElement() is always a single WebElement
object.
The findElement() method returns an object of the type
WebElement. It uses various locators such as ID, Xpath, CSS,
Name and ClassName.
Below is the syntax of findElement command:
WebElement elementName =
driver.findElement(By.LocatorStrategy("LocatorValue"));
Locator Strategy comprises any one of the locators. Locator value
is the unique value that identifies the web element.
2. FindElements():
● findElements() is a method provided by the WebDriver
interface in Selenium.
● It is used to locate all the web elements that match the
specified locator strategy and conditions.
● If there are no matching elements, it returns an empty list.
● The return type of findElements() is a list of WebElement
objects.
The findElements() return an empty list if no matching elements
are found on the web page.
Syntax in Java:
Below is the syntax of findElements command
List<WebElement> elementName =
driver.findElements(By.LocatorStrategy("LocatorValue"));
Locator Strategy comprises any one of the locators. Locator value
is the unique value that identifies the web element.
Now let us see the differences between both methods:
Difference between find Element and find
Elements in Selenium
Find Element Find Elements
Exceptio
n
Handlin
g
Throws a
NoSuchElementException
when no matching element
is found.
Returns an empty list
(not null) when no
matching elements are
found.
Purpose Find the first matching
element based on the
locator you specify.
Finds all matching
elements based on the
locator.
Return
Type
A single WebElement
object.
A List of WebElements.
If no elements match, it
returns an empty list.
Use
Case
When interacting with a
single element
When working with
multiple elements or
checking if elements
exist
Now let us see use cases for each of the following methods:
How to use Selenium findElement
Let us see firstly how to use Selenium findElement method:
In the above screenshot, we are finding the unique locator for the
Google Search text box.
As you can see , it has the unique ID , using the ID , we have
created Xpath as below:
//textarea[@id=’APjFqb’]
Below is the code snippet in java:
driver.get("https://www.google.com/")
WebElement element =
driver.findElement(By.xpath("//textarea[@id='APjFqb']"));
#NoSuchElementException if no matching Element is found on
the page.
Now let us see the use case of the
findElements method in Selenium Java:
In the above screenshot, for the registration form of IRCTC
application, we have a locator class with value “col-xs-12
inputBoxPad” which matches with 8 web elements on the web
page.
Below is the code in Java Selenium:
driver.get("https://www.irctc.co.in/nget/profile/user-signup");
List<WebElement> elements =
driver.findElements(By.className("col-xs-12 inputBoxPad"));
System.out.println("Number of elements:" +elements.size());
for(int i=0; i<elements.size(); i++){
System.out.println("Input field:" +
elements.get(i).getAttribute("value"));
Multiple Locator Methods Using findElement
Method:
Selenium WebDriver provides the findElement(By) method to
locate and interact with web elements in a script. The find
Element method uses the locator object “By”. There are many
ways of using “By” with locators based on the requirement.
1. By ID:
driver.findElement(By.id(<element ID>))
2. By Xpath:
driver.findElement(By.xpath(<xpath>))
3. By Name:
driver.findElement(By.name(<element-name>))
4. By Class Name:
driver.findElement(By.className(“<element-class>”))
5. By CSS Selector:
driver.findElement(By.cssSelector(<css-selector>))
6. By LinkText:
driver.findElement(By.linkText(<link text>))
Conclusion
understanding the differences between findElement() and
findElements() in SeleniumWebdriver Java is essential. These
two methods have different purposes and have unique use cases
while automating web based applications.
In brief, findElement() is used to locate and interact with a single
element, throwing an exception info Element is matching.
findElements() is used to locate multiple elements, returning an
empty list if none are matching.
Based on our needs, we can use either of them and create
efficient automation scripts.
Hope you liked it, Happy Testing!!
Source: This article was originally published at testgrid.io.

More Related Content

PPTX
Locators and webdriver commands Selenium Session2.pptx
PPTX
Selenium locators: ID, Name, xpath, CSS Selector advance methods
PDF
Using XPath in Selenium - All you need to know.pdf
PPTX
Selenium-Locators
PPTX
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
PPTX
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
PDF
How to use selenium locators effectively for web automation.pdf
PPTX
Locators in selenium - BNT 09
Locators and webdriver commands Selenium Session2.pptx
Selenium locators: ID, Name, xpath, CSS Selector advance methods
Using XPath in Selenium - All you need to know.pdf
Selenium-Locators
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
How to use selenium locators effectively for web automation.pdf
Locators in selenium - BNT 09

Similar to findElement and findElements in Selenium_ Use Cases with Examples.pdf (20)

PPTX
Selenium Locators
PPT
Web testing with selenium and by quontra solutions
PPTX
Automated_Testing_Selenium
PDF
Using XPath in Selenium_ All you need to know.pdf
PPTX
Automatedtestingwithselenium shubham jain
PPTX
Automation Testing by Selenium Web Driver
PDF
Selenium Overview
PDF
Interview Question & Answers for Selenium Freshers | LearningSlot
PPTX
xpaths.pptx
PDF
Understanding CSS Selectors in Selenium.pdf
PPTX
Software testing lab 3 &amp; 4 (2)
PPTX
Selenium training
PDF
Efficient Rails Test-Driven Development - Week 6
PDF
2010 07-18.wa.rails tdd-6
PPTX
Automated testing with selenium prasad bapatla
PDF
Selenium Introduction by Sandeep Sharda
PPTX
Mastering Test Automation: How To Use Selenium Successfully
PDF
Real World Selenium
PDF
Real World Selenium Testing
PPTX
Automation Testing
Selenium Locators
Web testing with selenium and by quontra solutions
Automated_Testing_Selenium
Using XPath in Selenium_ All you need to know.pdf
Automatedtestingwithselenium shubham jain
Automation Testing by Selenium Web Driver
Selenium Overview
Interview Question & Answers for Selenium Freshers | LearningSlot
xpaths.pptx
Understanding CSS Selectors in Selenium.pdf
Software testing lab 3 &amp; 4 (2)
Selenium training
Efficient Rails Test-Driven Development - Week 6
2010 07-18.wa.rails tdd-6
Automated testing with selenium prasad bapatla
Selenium Introduction by Sandeep Sharda
Mastering Test Automation: How To Use Selenium Successfully
Real World Selenium
Real World Selenium Testing
Automation Testing
Ad

More from Steve Wortham (20)

PDF
Selenium Testing The Complete Step-by-Step Tutorial.pdf
PDF
The SAP Testing A Comprehensive Guide.pdf
PDF
The Ultimate Guide to Salesforce Automation.pdf
PDF
Top AI Testing Tools to Streamline Your Automation Efforts.pdf
PDF
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
PDF
Streamlining Enterprise Demands Selecting the Ideal Cloud Test Automation.pdf
PDF
Geolocation Testing for Global Success_ Test from Anywhere.pdf
PDF
The Next Wave of Software Testing_ Trends Shaping 2025.pdf
PDF
Creating an Effective Enterprise Testing Strategy_ Best Practices and Conside...
PDF
How to Inspect Elements on Android Devices.pdf
PDF
GUI Testing_ Best Practices, Tools, and Checklists You Can’t Miss.pdf
PDF
Introducing TestGrid’s Private Device Lab.pdf
PDF
Scriptless Test Automation_ A Complete Guide.pdf
PDF
Top iOS Testing Tools and Frameworks.pdf
PDF
The Test Cases for E-commerce Website.pdf
PDF
Playwright and its Installation Guide.pdf
PDF
A Guide to Codeless Automation on iPhone Devices.pdf
PDF
Understanding DevOps, its benefits, and best practices.pdf
PDF
Boost Your Telecom Testing Strategy_ Steps to Achieve Seamless Connectivity.pdf
PDF
A Complete Step-by-Step Guide to Mobile App Performance Testing.pdf
Selenium Testing The Complete Step-by-Step Tutorial.pdf
The SAP Testing A Comprehensive Guide.pdf
The Ultimate Guide to Salesforce Automation.pdf
Top AI Testing Tools to Streamline Your Automation Efforts.pdf
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Streamlining Enterprise Demands Selecting the Ideal Cloud Test Automation.pdf
Geolocation Testing for Global Success_ Test from Anywhere.pdf
The Next Wave of Software Testing_ Trends Shaping 2025.pdf
Creating an Effective Enterprise Testing Strategy_ Best Practices and Conside...
How to Inspect Elements on Android Devices.pdf
GUI Testing_ Best Practices, Tools, and Checklists You Can’t Miss.pdf
Introducing TestGrid’s Private Device Lab.pdf
Scriptless Test Automation_ A Complete Guide.pdf
Top iOS Testing Tools and Frameworks.pdf
The Test Cases for E-commerce Website.pdf
Playwright and its Installation Guide.pdf
A Guide to Codeless Automation on iPhone Devices.pdf
Understanding DevOps, its benefits, and best practices.pdf
Boost Your Telecom Testing Strategy_ Steps to Achieve Seamless Connectivity.pdf
A Complete Step-by-Step Guide to Mobile App Performance Testing.pdf
Ad

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
KodekX | Application Modernization Development
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
KodekX | Application Modernization Development
Digital-Transformation-Roadmap-for-Companies.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Monthly Chronicles - July 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Understanding_Digital_Forensics_Presentation.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

findElement and findElements in Selenium_ Use Cases with Examples.pdf

  • 1. findElement and findElements in Selenium U S E C A S E S W I T H E X A M P L E S
  • 2. findElement and findElements in Selenium: Use Cases with Examples When we want to interact with any web page, we use locators to identify the web elements and interact with them using locators. We use HTML elements on the web page while automating any web based application using Selenium WebDriver. Selenium WebDriver has two methods for identifying the web elements, they are findElement and findElements. 1. findElement: This is used to uniquely identify any web element within the web page. 2. findElements: This is used to uniquely identify the list of web elements within the web page.
  • 3. These methods are very important for locating web elements on a web page, but they serve different purposes and have distinct use cases. Let us see what exactly the both methods are: 1. Find Element(): ● findElement() is a method in the WebDriver Interface in Selenium. ● It is used to locate the first element that matches the specified locator strategy and conditions. ● If no matching element is found, it throws a NoSuch Element Exception. ● The result of findElement() is always a single WebElement object. The findElement() method returns an object of the type WebElement. It uses various locators such as ID, Xpath, CSS, Name and ClassName. Below is the syntax of findElement command: WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue")); Locator Strategy comprises any one of the locators. Locator value is the unique value that identifies the web element. 2. FindElements():
  • 4. ● findElements() is a method provided by the WebDriver interface in Selenium. ● It is used to locate all the web elements that match the specified locator strategy and conditions. ● If there are no matching elements, it returns an empty list. ● The return type of findElements() is a list of WebElement objects. The findElements() return an empty list if no matching elements are found on the web page. Syntax in Java: Below is the syntax of findElements command List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue")); Locator Strategy comprises any one of the locators. Locator value is the unique value that identifies the web element. Now let us see the differences between both methods: Difference between find Element and find Elements in Selenium Find Element Find Elements Exceptio n Handlin g Throws a NoSuchElementException when no matching element is found. Returns an empty list (not null) when no matching elements are found. Purpose Find the first matching element based on the locator you specify. Finds all matching elements based on the locator.
  • 5. Return Type A single WebElement object. A List of WebElements. If no elements match, it returns an empty list. Use Case When interacting with a single element When working with multiple elements or checking if elements exist Now let us see use cases for each of the following methods: How to use Selenium findElement Let us see firstly how to use Selenium findElement method: In the above screenshot, we are finding the unique locator for the Google Search text box.
  • 6. As you can see , it has the unique ID , using the ID , we have created Xpath as below: //textarea[@id=’APjFqb’] Below is the code snippet in java: driver.get("https://www.google.com/") WebElement element = driver.findElement(By.xpath("//textarea[@id='APjFqb']")); #NoSuchElementException if no matching Element is found on the page. Now let us see the use case of the findElements method in Selenium Java:
  • 7. In the above screenshot, for the registration form of IRCTC application, we have a locator class with value “col-xs-12 inputBoxPad” which matches with 8 web elements on the web page. Below is the code in Java Selenium: driver.get("https://www.irctc.co.in/nget/profile/user-signup"); List<WebElement> elements = driver.findElements(By.className("col-xs-12 inputBoxPad")); System.out.println("Number of elements:" +elements.size()); for(int i=0; i<elements.size(); i++){ System.out.println("Input field:" + elements.get(i).getAttribute("value")); Multiple Locator Methods Using findElement Method: Selenium WebDriver provides the findElement(By) method to locate and interact with web elements in a script. The find Element method uses the locator object “By”. There are many ways of using “By” with locators based on the requirement. 1. By ID: driver.findElement(By.id(<element ID>))
  • 8. 2. By Xpath: driver.findElement(By.xpath(<xpath>)) 3. By Name: driver.findElement(By.name(<element-name>)) 4. By Class Name: driver.findElement(By.className(“<element-class>”)) 5. By CSS Selector: driver.findElement(By.cssSelector(<css-selector>)) 6. By LinkText: driver.findElement(By.linkText(<link text>)) Conclusion understanding the differences between findElement() and findElements() in SeleniumWebdriver Java is essential. These two methods have different purposes and have unique use cases while automating web based applications. In brief, findElement() is used to locate and interact with a single element, throwing an exception info Element is matching.
  • 9. findElements() is used to locate multiple elements, returning an empty list if none are matching. Based on our needs, we can use either of them and create efficient automation scripts. Hope you liked it, Happy Testing!! Source: This article was originally published at testgrid.io.