SlideShare a Scribd company logo
How to find
Xpath of an
element??
Mithilesh Singh
Locators & xpath
Locators
 identification of the
correct GUI element on a web page is
pre-requisite for creating any
successful automation script. It is
where locators come into the
picture. Locators are one of the
essential components of Selenium
infrastructure, which help Selenium
scripts in uniquely identifying
the WebElements(such as text box,
button, etc.)
 Types of locators:
ID,
Class,
Tag, Link,
Partial Link Text,
CSS, Name,
XPath.
What is DOM?
 The Document Object Model
(DOM) is a programming API
for HTML and XML
documents. It defines the
logical structure of
documents and the way a
document is accessed and
manipulated. ... Nevertheless,
XML presents this data as
documents, and
the DOM may be used to
manage this data.
XPath in
Selenium:
 XPath is a technique in Selenium to navigate
through the HTML structure of a page. XPath enables
testers to navigate through the XML structure of any
document, and this can be used on both HTML and
XML documents.
 1. While Selenium has wrappers for most popular
programming languages, the selector string remains
the same for all of them.
 2. While other locators in Selenium which search for
elements using tags or CSS class names are simpler
to use, they may not be sufficient to select all DOM
elements of an HTML document.
 XPath is defined as XML path. It is a syntax or
language for finding any element on the web page
using the XML path expression.
Syntax of Xpath
 XPath =
//tag_name[@Attribute_name =
“Value of attribute”]
 // --> Select current node.
 tag_name --> Tagname of the
particular node.
 @ --> Select attribute.
 Attribute_name --> Attribute name
of the node.
 Value of attribute --> Value of the
attribute.
Types of XPath
Absolute Xpath
 Absolute XPath is the direct way of
finding the element. Moreover, it
starts from the first/root node of
the XML/HTML document and
goes all the way to the required
node following one node at a
time.
Example:
/html/body/div/header/a/img
Relative Xpath
 Relative XPath starts from any node inside
the HTML DOM; it need not start from
the root node. It beings with a double
forward slash.
Example:
//img[@src= "/images/Testerszone.jpg"]
Relative Xpath Syntax for Pre - defined locators:
 Using text() : //*[text()='testers zone']
 Using name() : //input[@name='Mithilesh']
 Using id() : //input[@id='user-message‘]
 Using class() : //input[@class='user-message']
 LinkText() : //a[@href='http://testerszone.com/']
Note: We can use * in place of input, it will also work fine. Input is specific
tag name but * is generic(point out all the available tags in the DOM) we
can use for any tag name.
Key Points:
We can directly use the pre defined
locators in findElement method instead
of xpath.
driver.findElement(By.id("Testers Zone"));
Note:
We can use other locators like name,
className etc in same way.
You will get to know all available locators using
driver.findElement(By. );
Use of Contains() in Xpath
• Contains() method always use in xpath to get the element using partial text.
• Whenever we have long text or dynamic text we go with contains().
Using text() : //*[contains(text(),'testers ')]
Using name() : //input[contains(@name,'Mith')]
Using id() : //input[contains(@id,'user-message‘)]
Using class() : //input[contains(@class,'user-message')]
Partial-LinkText() : //a[contains(@href,'testerszone.com']
Use of
Starts-with()
 starts-with() method is used when we know
about the initial partial attribute value or
initial partial text associated with the web
element.
Syntax:
//a[starts-with(@id,'link-testers_')]
Note: inside the ' ' you have to mention the
partial text value, make sure you are getting
unique matching element with your xpath.
Simillar way we have ends_with() also. We use
end partial part of text.
Use of
OR & AND

Xpath=//input[@type='submit' and @name='btnLogin']
XPath axes
method
Sometimes we don't get element very easily so we need to use axes method in xpath, we use this to find complex and dynamic
element also.
1. Following:
This will give you the count of total elements in a document of the current node and we can access them using index.
Syntax:
Xpath=//*[@type='text']//following::input
2.Ancestor The ancestor axis selects all ancestors element (parent, grandparent, great-
grandparents, etc.) of the current node.
Syntax:
Xpath=//*[@type='text']//ancestor::div
3. Child
Select all the child elements of the current node.
Xpath= //*[@class='ng-lns-c59-10']//child::tr
4. Preceding
Select all nodes that come before the current
node
Xpath= //*[@type='password']//preceding::input
5. Following-sibling:
The following-sibling selects all sibling nodes after the current node at the same level. i.e. It will find the element
after the current node.
Xpath=//div[@id='nlplmgContainer']//following-
sibling::input
6. Parent
It select the parent of the current node.
Xpath= //*[@type='password']//parent::div
7. Descendant It select below child and grand child node of the curent node.
Note: There is small difference between descendant and following, following gives all the
mathing node in the document and descendant gives elements of current node only.
Introduction- CSS Selector
 CSS stands for Cascading spreadsheets, it is a stylesheet language use to describe
the presentation of the document written in mark up language like HTML
 We use CSS selector as a locator in the selenium
 It is very fast as compare to other locators because it uses certain symbol
Selenium-Locators
There are three important special characters in css selectors:
1. '^' symbol, represents the starting text in a string.
2. '$' symbol represents the ending text in a string.
3. '*' symbol represents contains text in a string.
Partial Value is nothing but partial linked text, In CSS
we use * for this.
Example
 css = input[id$='mail']
 css = input[id*='mai']
 css = input[id^='ema']
 using child selectors--> css = form>label>input[id=PersistentCookie]
 using multiple node --> css = input[name=email][type=text]
Key Points:
Xpath :
 XPath stands for XML Path
 XPath is used to find the element in the HTML
DOM
 The success rate of finding an element using
Xpath is too high
 XPath is used where element has no other way
of locating
 Locate elements in forward and backward
direction.
 Can locate element by text
 Starts with / or //
 More flexible
CSS Selector :
 CSS stands for cascading style sheet
 CSS Selector is used to find the element in the
HTML DOM using style sheet language.
 The success rate of finding an element using
CSS Selector is less compare to Xpath.
 do not support all CSS features.
 Faster in all browsers
 Can not locate by element by text
 Locate elements only in forward direction
 Some CSS selectors will not work all browsers
By Mithilesh Singh

More Related Content

PPTX
Selenium locators: ID, Name, xpath, CSS Selector advance methods
PDF
Automation Testing using Selenium Webdriver
PPTX
Automation Testing by Selenium Web Driver
PPTX
An overview of selenium webdriver
PPTX
Introduction to Selenium Web Driver
PPTX
Selenium WebDriver training
PPTX
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
PPT
QSpiders - Automation using Selenium
Selenium locators: ID, Name, xpath, CSS Selector advance methods
Automation Testing using Selenium Webdriver
Automation Testing by Selenium Web Driver
An overview of selenium webdriver
Introduction to Selenium Web Driver
Selenium WebDriver training
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
QSpiders - Automation using Selenium

What's hot (20)

PPTX
Selenium Locators
PDF
Selenium IDE LOCATORS
PDF
TestNG Annotations in Selenium | Edureka
PPTX
Test automation using selenium
PPT
Selenium Automation Framework
PPTX
Hybrid automation framework
PPT
Test automation using selenium
PPTX
Data driven Automation Framework with Selenium
PPTX
Test Automation and Selenium
PPTX
Automation - web testing with selenium
PDF
Page Object Model and Implementation in Selenium
PPT
Ppt of soap ui
PPT
Selenium
PPT
Selenium Concepts
PPTX
TestNG Framework
PDF
Selenium WebDriver with C#
PPTX
TestNG with selenium
PDF
Waits in Selenium | Selenium Wait Commands | Edureka
PPTX
Selenium WebDriver
PPS
Selenium Demo
Selenium Locators
Selenium IDE LOCATORS
TestNG Annotations in Selenium | Edureka
Test automation using selenium
Selenium Automation Framework
Hybrid automation framework
Test automation using selenium
Data driven Automation Framework with Selenium
Test Automation and Selenium
Automation - web testing with selenium
Page Object Model and Implementation in Selenium
Ppt of soap ui
Selenium
Selenium Concepts
TestNG Framework
Selenium WebDriver with C#
TestNG with selenium
Waits in Selenium | Selenium Wait Commands | Edureka
Selenium WebDriver
Selenium Demo
Ad

Similar to Selenium-Locators (20)

PDF
Using XPath in Selenium - All you need to know.pdf
PDF
Using XPath in Selenium_ All you need to know.pdf
PPTX
xpaths.pptx
PPTX
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
PPTX
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
PDF
Understanding CSS Selectors in Selenium.pdf
PDF
How to use selenium locators effectively for web automation.pdf
PDF
Hands-on Guide to Object Identification
PPTX
Automatedtestingwithselenium shubham jain
PPTX
Extracting data from xml
PPTX
Automated_Testing_Selenium
PPTX
Structured Strategy: How to Supercharge Your Content Analysis with XML and XPath
PPTX
X path
PPTX
X path
PDF
Xpath.pdf
PDF
A brief overview of XPath - Topic in XML - Web Technologies
PDF
CSS Selector in Selenium WebDriver | Edureka
PDF
Xpath tutorial
PPTX
Locators and webdriver commands Selenium Session2.pptx
Using XPath in Selenium - All you need to know.pdf
Using XPath in Selenium_ All you need to know.pdf
xpaths.pptx
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
Agile_unit_2_newdddcdcdscdssvvdvdsvdsv.pptx
Understanding CSS Selectors in Selenium.pdf
How to use selenium locators effectively for web automation.pdf
Hands-on Guide to Object Identification
Automatedtestingwithselenium shubham jain
Extracting data from xml
Automated_Testing_Selenium
Structured Strategy: How to Supercharge Your Content Analysis with XML and XPath
X path
X path
Xpath.pdf
A brief overview of XPath - Topic in XML - Web Technologies
CSS Selector in Selenium WebDriver | Edureka
Xpath tutorial
Locators and webdriver commands Selenium Session2.pptx
Ad

More from Mithilesh Singh (20)

PPTX
End2EndTesting_With_GenerativeAI - ChatGPT
PPTX
GITHUB_ACTIONS_CICD_OVERVIEW_FOR_BEGINNERS
PDF
Cucumber Basics.pdf
PDF
Data Migration.pdf
PDF
SDLC Models.pdf
PDF
Test_Case_Design_Techniques
PDF
Performance Testing
PDF
Software_requirement_collection
PDF
Stub_&_Drive
PDF
Functional_Testing_Part-1
PDF
TestersMindSet 2022
PDF
API_Testing_with_Postman
PDF
Agile_basics
PDF
Selenium_Grid
PDF
Appium_set_up
PDF
Appium- part 1
PDF
Alpha & Beta Testing
PDF
Severity and Priority
PDF
GIT_Overview.
PDF
Browser_Stack_Intro
End2EndTesting_With_GenerativeAI - ChatGPT
GITHUB_ACTIONS_CICD_OVERVIEW_FOR_BEGINNERS
Cucumber Basics.pdf
Data Migration.pdf
SDLC Models.pdf
Test_Case_Design_Techniques
Performance Testing
Software_requirement_collection
Stub_&_Drive
Functional_Testing_Part-1
TestersMindSet 2022
API_Testing_with_Postman
Agile_basics
Selenium_Grid
Appium_set_up
Appium- part 1
Alpha & Beta Testing
Severity and Priority
GIT_Overview.
Browser_Stack_Intro

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Introduction to Artificial Intelligence
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
top salesforce developer skills in 2025.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Transform Your Business with a Software ERP System
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Wondershare Filmora 15 Crack With Activation Key [2025
Upgrade and Innovation Strategies for SAP ERP Customers
Introduction to Artificial Intelligence
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
top salesforce developer skills in 2025.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Transform Your Business with a Software ERP System
Reimagine Home Health with the Power of Agentic AI​
VVF-Customer-Presentation2025-Ver1.9.pptx
Odoo POS Development Services by CandidRoot Solutions
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
Design an Analysis of Algorithms I-SECS-1021-03
Design an Analysis of Algorithms II-SECS-1021-03
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How Creative Agencies Leverage Project Management Software.pdf
Softaken Excel to vCard Converter Software.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

Selenium-Locators

  • 1. How to find Xpath of an element?? Mithilesh Singh Locators & xpath
  • 2. Locators  identification of the correct GUI element on a web page is pre-requisite for creating any successful automation script. It is where locators come into the picture. Locators are one of the essential components of Selenium infrastructure, which help Selenium scripts in uniquely identifying the WebElements(such as text box, button, etc.)  Types of locators: ID, Class, Tag, Link, Partial Link Text, CSS, Name, XPath.
  • 3. What is DOM?  The Document Object Model (DOM) is a programming API for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated. ... Nevertheless, XML presents this data as documents, and the DOM may be used to manage this data.
  • 4. XPath in Selenium:  XPath is a technique in Selenium to navigate through the HTML structure of a page. XPath enables testers to navigate through the XML structure of any document, and this can be used on both HTML and XML documents.  1. While Selenium has wrappers for most popular programming languages, the selector string remains the same for all of them.  2. While other locators in Selenium which search for elements using tags or CSS class names are simpler to use, they may not be sufficient to select all DOM elements of an HTML document.  XPath is defined as XML path. It is a syntax or language for finding any element on the web page using the XML path expression.
  • 5. Syntax of Xpath  XPath = //tag_name[@Attribute_name = “Value of attribute”]  // --> Select current node.  tag_name --> Tagname of the particular node.  @ --> Select attribute.  Attribute_name --> Attribute name of the node.  Value of attribute --> Value of the attribute.
  • 6. Types of XPath Absolute Xpath  Absolute XPath is the direct way of finding the element. Moreover, it starts from the first/root node of the XML/HTML document and goes all the way to the required node following one node at a time. Example: /html/body/div/header/a/img Relative Xpath  Relative XPath starts from any node inside the HTML DOM; it need not start from the root node. It beings with a double forward slash. Example: //img[@src= "/images/Testerszone.jpg"]
  • 7. Relative Xpath Syntax for Pre - defined locators:  Using text() : //*[text()='testers zone']  Using name() : //input[@name='Mithilesh']  Using id() : //input[@id='user-message‘]  Using class() : //input[@class='user-message']  LinkText() : //a[@href='http://testerszone.com/'] Note: We can use * in place of input, it will also work fine. Input is specific tag name but * is generic(point out all the available tags in the DOM) we can use for any tag name.
  • 8. Key Points: We can directly use the pre defined locators in findElement method instead of xpath. driver.findElement(By.id("Testers Zone")); Note: We can use other locators like name, className etc in same way. You will get to know all available locators using driver.findElement(By. );
  • 9. Use of Contains() in Xpath • Contains() method always use in xpath to get the element using partial text. • Whenever we have long text or dynamic text we go with contains(). Using text() : //*[contains(text(),'testers ')] Using name() : //input[contains(@name,'Mith')] Using id() : //input[contains(@id,'user-message‘)] Using class() : //input[contains(@class,'user-message')] Partial-LinkText() : //a[contains(@href,'testerszone.com']
  • 10. Use of Starts-with()  starts-with() method is used when we know about the initial partial attribute value or initial partial text associated with the web element. Syntax: //a[starts-with(@id,'link-testers_')] Note: inside the ' ' you have to mention the partial text value, make sure you are getting unique matching element with your xpath. Simillar way we have ends_with() also. We use end partial part of text.
  • 11. Use of OR & AND  Xpath=//input[@type='submit' and @name='btnLogin']
  • 12. XPath axes method Sometimes we don't get element very easily so we need to use axes method in xpath, we use this to find complex and dynamic element also. 1. Following: This will give you the count of total elements in a document of the current node and we can access them using index. Syntax: Xpath=//*[@type='text']//following::input
  • 13. 2.Ancestor The ancestor axis selects all ancestors element (parent, grandparent, great- grandparents, etc.) of the current node. Syntax: Xpath=//*[@type='text']//ancestor::div
  • 14. 3. Child Select all the child elements of the current node. Xpath= //*[@class='ng-lns-c59-10']//child::tr
  • 15. 4. Preceding Select all nodes that come before the current node Xpath= //*[@type='password']//preceding::input
  • 16. 5. Following-sibling: The following-sibling selects all sibling nodes after the current node at the same level. i.e. It will find the element after the current node. Xpath=//div[@id='nlplmgContainer']//following- sibling::input
  • 17. 6. Parent It select the parent of the current node. Xpath= //*[@type='password']//parent::div
  • 18. 7. Descendant It select below child and grand child node of the curent node. Note: There is small difference between descendant and following, following gives all the mathing node in the document and descendant gives elements of current node only.
  • 19. Introduction- CSS Selector  CSS stands for Cascading spreadsheets, it is a stylesheet language use to describe the presentation of the document written in mark up language like HTML  We use CSS selector as a locator in the selenium  It is very fast as compare to other locators because it uses certain symbol
  • 21. There are three important special characters in css selectors: 1. '^' symbol, represents the starting text in a string. 2. '$' symbol represents the ending text in a string. 3. '*' symbol represents contains text in a string.
  • 22. Partial Value is nothing but partial linked text, In CSS we use * for this.
  • 23. Example  css = input[id$='mail']  css = input[id*='mai']  css = input[id^='ema']  using child selectors--> css = form>label>input[id=PersistentCookie]  using multiple node --> css = input[name=email][type=text]
  • 24. Key Points: Xpath :  XPath stands for XML Path  XPath is used to find the element in the HTML DOM  The success rate of finding an element using Xpath is too high  XPath is used where element has no other way of locating  Locate elements in forward and backward direction.  Can locate element by text  Starts with / or //  More flexible CSS Selector :  CSS stands for cascading style sheet  CSS Selector is used to find the element in the HTML DOM using style sheet language.  The success rate of finding an element using CSS Selector is less compare to Xpath.  do not support all CSS features.  Faster in all browsers  Can not locate by element by text  Locate elements only in forward direction  Some CSS selectors will not work all browsers