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.
Ad

More Related Content

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

Introduction to selenium web driver
Introduction to selenium web driverIntroduction to selenium web driver
Introduction to selenium web driver
Return on Intelligence
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
Dev labs alliance top 50 selenium interview questions for SDET
Dev labs alliance top 50 selenium interview questions for SDETDev labs alliance top 50 selenium interview questions for SDET
Dev labs alliance top 50 selenium interview questions for SDET
devlabsalliance
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
Vinay Kumar Pulabaigari
 
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Iakiv Kramarenko
 
Latest Selenium Interview Questions And Answers.pdf
Latest Selenium Interview Questions And Answers.pdfLatest Selenium Interview Questions And Answers.pdf
Latest Selenium Interview Questions And Answers.pdf
Varsha Rajput
 
Selenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersSelenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And Answers
Ajit Jadhav
 
Web Development Course - JQuery by RSOLUTIONS
Web Development Course - JQuery by RSOLUTIONSWeb Development Course - JQuery by RSOLUTIONS
Web Development Course - JQuery by RSOLUTIONS
RSolutions
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
kavinilavuG
 
Introduction to selenium web driver
Introduction to selenium web driverIntroduction to selenium web driver
Introduction to selenium web driver
Return on Intelligence
 
Using XPath in Selenium_ All you need to know.pdf
Using XPath in Selenium_ All you need to know.pdfUsing XPath in Selenium_ All you need to know.pdf
Using XPath in Selenium_ All you need to know.pdf
RobertMartin69776
 
xpaths.pptx
xpaths.pptxxpaths.pptx
xpaths.pptx
KundetiLeena1
 
Escape from the automation hell
Escape from the automation hellEscape from the automation hell
Escape from the automation hell
Nikita Simonovets
 
Selenium Webdriver Interview Questions
Selenium Webdriver Interview QuestionsSelenium Webdriver Interview Questions
Selenium Webdriver Interview Questions
Jai Singh
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Seble Nigussie
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
Return on Intelligence
 
Selenium inputs
Selenium inputsSelenium inputs
Selenium inputs
KadarkaraiSelvam
 
Test automation with selenide
Test automation with selenideTest automation with selenide
Test automation with selenide
Isuru Madanayaka
 
Understanding CSS Selectors in Selenium.pdf
Understanding CSS Selectors in Selenium.pdfUnderstanding CSS Selectors in Selenium.pdf
Understanding CSS Selectors in Selenium.pdf
pCloudy
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
Dev labs alliance top 50 selenium interview questions for SDET
Dev labs alliance top 50 selenium interview questions for SDETDev labs alliance top 50 selenium interview questions for SDET
Dev labs alliance top 50 selenium interview questions for SDET
devlabsalliance
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
Vinay Kumar Pulabaigari
 
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Iakiv Kramarenko
 
Latest Selenium Interview Questions And Answers.pdf
Latest Selenium Interview Questions And Answers.pdfLatest Selenium Interview Questions And Answers.pdf
Latest Selenium Interview Questions And Answers.pdf
Varsha Rajput
 
Selenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersSelenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And Answers
Ajit Jadhav
 
Web Development Course - JQuery by RSOLUTIONS
Web Development Course - JQuery by RSOLUTIONSWeb Development Course - JQuery by RSOLUTIONS
Web Development Course - JQuery by RSOLUTIONS
RSolutions
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
kavinilavuG
 
Using XPath in Selenium_ All you need to know.pdf
Using XPath in Selenium_ All you need to know.pdfUsing XPath in Selenium_ All you need to know.pdf
Using XPath in Selenium_ All you need to know.pdf
RobertMartin69776
 
Escape from the automation hell
Escape from the automation hellEscape from the automation hell
Escape from the automation hell
Nikita Simonovets
 
Selenium Webdriver Interview Questions
Selenium Webdriver Interview QuestionsSelenium Webdriver Interview Questions
Selenium Webdriver Interview Questions
Jai Singh
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Seble Nigussie
 
Test automation with selenide
Test automation with selenideTest automation with selenide
Test automation with selenide
Isuru Madanayaka
 
Understanding CSS Selectors in Selenium.pdf
Understanding CSS Selectors in Selenium.pdfUnderstanding CSS Selectors in Selenium.pdf
Understanding CSS Selectors in Selenium.pdf
pCloudy
 

More from Steve Wortham (20)

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

Recently uploaded (20)

Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Ad

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.