Xpath Guideline and Practises
Xpath Guideline and Practises
Quang Le
CONTENT
1. INTRODUCTION
2. SYNTAX
3. BASIC OPERATORS AND
FUNCTIONS
4. EXAMPLES
INTRODUCTION
INTRODUCTION
WHAT IS XPATH?
INTRODUCTION
+ Addition 6+4 10
- Subtraction 6-4 2
* Multiplication 6*4 24
div Division 8 div 4 2
= Equal price=9.80 true if price is 9.80
false if price is 9.90
preceding Selects all nodes that appear before the current node in the
document, except ancestors, attribute nodes and namespace
nodes
preceding-sibling Selects all siblings before the current node
Results
<AAA> <AAA>
<BBB/> <BBB/>
<BBB/> <BBB/>
<BBB/> <BBB/>
<BBB/> <BBB/>
</AAA> </AAA>
ATTRIBUTE @
Intro Attributes are specified by @ prefix.
XPath //@id //BBB[@id]
Target Select all attributes @id Select BBB elements which have attribute id
Results
<AAA> <AAA>
<BBB id = "b1"/> <BBB id = "b1"/>
<BBB id = "b2"/> <BBB id = "b2"/>
<BBB name = "bbb"/> <BBB name = "bbb"/>
<BBB/> <BBB/>
</AAA> </AAA>
ATTRIBUTE @
Intro Attributes are specified by @ prefix.
XPath //BBB[@*] //BBB[not(@*)]
Target Select BBB elements which have any attribute Select BBB elements without an attribute
Results
<AAA> <AAA>
<BBB id = "b1"/> <BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/> <BBB id = "b2"/>
<BBB/> <BBB name = "bbb"/>
</AAA> <BBB/>
</AAA>
VALUES OF ATTRIBUTES
Intro Values of attributes can be used as selection criteria.
Function normalize-space removes leading and trailing spaces and replaces sequences of
whitespace characters by a single space.
XPath //BBB[@name='bbb'] //BBB[normalize-space(@name)='bbb']
Target Select BBB elements which have attribute Select BBB elements which have attribute name
name with value 'bbb' with value bbb, leading and trailing spaces are
removed before comparison
Results
<AAA> <AAA>
<BBB id = "b1"/> <BBB id = "b1"/>
<BBB name = " bbb "/> <BBB name = " bbb "/>
<BBB name = "bbb"/> <BBB name = "bbb"/>
</AAA> </AAA>
COUNT()
Intro Function count() counts the number of selected elements
XPath //*[count(BBB)=2] //*[count(*)=2]
Target Select elements which have two children BBB Select elements which have 2 children
Results
<AAA> <AAA>
<CCC> <CCC>
<BBB/> <BBB/>
<BBB/> <BBB/>
<BBB/> <BBB/>
</CCC> </CCC>
<DDD> <DDD>
<BBB/> <BBB/>
<BBB/> <BBB/>
</DDD> </DDD>
<EEE> <EEE>
<CCC/> <CCC/>
<DDD/> <DDD/>
</EEE> </EEE>
</AAA> </AAA>
NAME ()
Intro Function name() returns name of the element
XPath //*[name()='BBB'] //*[starts-with(name(),'B')] //*[contains(name(),'C')]
Target Select all elements with
Select all elements name of which Select all elements name of which
name BBB, equivalent starts with letter B contain letter C
with //BBB
Results
<AAA> <AAA>
<BBB> <BBB>
<DDD> <DDD>
<CCC> <CCC>
<DDD/> <DDD/>
<EEE/> <EEE/>
</CCC> </CCC>
</DDD> </DDD>
</BBB> </BBB>
<CCC> <CCC>
<DDD> <DDD>
<EEE> <EEE>
<DDD> <DDD>
<F/> <F/>
</DDD> </DDD>
</EEE> </EEE>
</DDD> </DDD>
</CCC> </CCC>
</AAA> </AAA>
DESCENDANT
Intro The descendant axis contains the descendants of the context node
A descendant is a child or a child of a child and so on
Thus the descendant axis never contains attribute or namespace nodes
XPath /AAA/BBB/descendant::* //CCC/descendant::* //CCC/descendant::DDD
Target Select all descendants of /AAA/BBB Select all elements which have CCC among Select elements DDD which have CCC among
its ancestors its ancestors
Results
<AAA> <AAA> <AAA>
<BBB> <BBB> <BBB>
<DDD> <DDD> <DDD>
<CCC> <CCC> <CCC>
<DDD/> <DDD/> <DDD/>
<EEE/> <EEE/> <EEE/>
</CCC> </CCC> </CCC>
</DDD> </DDD> </DDD>
</BBB> </BBB> </BBB>
<CCC> <CCC> <CCC>
<DDD> <DDD> <DDD>
<EEE> <EEE> <EEE>
<DDD> <DDD> <DDD>
<FFF/> <FFF/>
</DDD> </DDD> </DDD>
</EEE> </EEE> </EEE>
</DDD> </DDD> </DDD>
</CCC> </CCC> </CCC>
</AAA> </AAA> </AAA>
DESCENDANT-OR-SELF
Intro The descendant-or-self axis contains the context node and the descendants of the context
node
XPath /AAA/XXX/descendant-or-self::* //CCC/descendant-or-self::*
Target
Results
<AAA> <AAA>
<BBB> <BBB>
<CCC/> <CCC/>
<ZZZ> <ZZZ>
<DDD/> <DDD/>
</ZZZ> </ZZZ>
</BBB> </BBB>
<XXX> <XXX>
<DDD> <DDD>
<EEE/> <EEE/>
<DDD/> <DDD/>
<CCC/> <CCC/>
<FFF/> <FFF/>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</DDD> </DDD>
</XXX> </XXX>
<CCC> <CCC>
<DDD/> <DDD/>
</CCC> </CCC>
</AAA> </AAA>
ANCESTOR
Intro The ancestor axis contains the ancestors of the context node
The ancestors of the context node consist of the parent of context node and the parent's parent and so
on
Thus, the ancestor axis will always include the root node, unless the context node is the root node.
Target Select all elements given in this absolute path Select ancestors of FFF element
Results
<AAA> <AAA>
<BBB> <BBB>
<DDD> <DDD>
<CCC> <CCC>
<DDD/> <DDD/>
<EEE/> <EEE/>
</CCC> </CCC>
</DDD> </DDD>
</BBB> </BBB>
<CCC> <CCC>
<DDD> <DDD>
<EEE> <EEE>
<DDD> <DDD>
<FFF/> <FFF/>
</DDD> </DDD>
</EEE> </EEE>
</DDD> </DDD>
</CCC> </CCC>
</AAA> </AAA>
ANCESTOR-OR-SELF
Intro The ancestor-or-self axis contains the context node and the ancestors of the context node
The ancestor-or-self axis will always include the root node
<AAA> <AAA>
<BBB> <BBB>
<CCC/> <CCC/>
<ZZZ> <ZZZ>
<DDD/> <DDD/>
</ZZZ> </ZZZ>
</BBB> </BBB>
<XXX> <XXX>
<DDD> <DDD>
<EEE/> <EEE/>
<DDD/> <DDD/>
<CCC/> <CCC/>
<FFF/> <FFF/>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</DDD> </DDD>
</XXX> </XXX>
<CCC> <CCC>
<DDD/> <DDD/>
</CCC> </CCC>
</AAA> </AAA>
FOLLOWING
Intro The following axis contains all nodes in the same document as the context node that are after the
context node in document order
XPath /AAA/XXX/following::* //ZZZ/following::*
Target
Results
<AAA> <AAA>
<BBB> <BBB>
<CCC/> <CCC/>
<ZZZ> <ZZZ>
<DDD/> <DDD/>
<DDD> <DDD>
<EEE/> <EEE/>
</DDD> </DDD>
</ZZZ> </ZZZ>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</BBB> </BBB>
<XXX> <XXX>
<DDD> <DDD>
<EEE/> <EEE/>
<DDD/> <DDD/>
<CCC/> <CCC/>
<FFF/> <FFF/>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</DDD> </DDD>
</XXX> </XXX>
<CCC> <CCC>
<DDD/> <DDD/>
</CCC> </CCC>
</AAA> </AAA>
FOLLOWING-SIBLING
Intro The following-sibling axis contains all the following siblings of the context node. (the same
level)
XPath /AAA/BBB/following-sibling::* //CCC/following-sibling::*
Target
Results
<AAA> <AAA>
<BBB> <BBB>
<CCC/> <CCC/>
<DDD/> <DDD/>
</BBB> </BBB>
<XXX> <XXX>
<DDD> <DDD>
<EEE/> <EEE/>
<DDD/> <DDD/>
<CCC/> <CCC/>
<FFF/> <FFF/>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</DDD> </DDD>
</XXX> </XXX>
<CCC> <CCC>
<DDD/> <DDD/>
</CCC> </CCC>
</AAA> </AAA>
PRECEDING
Intro The preceding axis contains all nodes in the same document as the context node that are before the
context node in document order
XPath /AAA/XXX/preceding::* //GGG/preceding::*
Target
Results
<AAA> <AAA>
<BBB> <BBB>
<CCC/> <CCC/>
<ZZZ> <ZZZ>
<DDD/> <DDD/>
</ZZZ> </ZZZ>
</BBB> </BBB>
<XXX> <XXX>
<DDD> <DDD>
<EEE/> <EEE/>
<DDD/> <DDD/>
<CCC/> <CCC/>
<FFF/> <FFF/>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</DDD> </DDD>
</XXX> </XXX>
<CCC> <CCC>
<DDD/> <DDD/>
</CCC> </CCC>
</AAA> </AAA>
PRECEDING-SIBLING
Intro The preceding-sibling axis contains all the preceding siblings of the context node(the same and smaller
level)
XPath /AAA/XXX/preceding-sibling::* //CCC/preceding-sibling::*
Target
Results
<AAA> <AAA>
<BBB> <BBB>
<CCC/> <CCC/>
<DDD/> <DDD/>
</BBB> </BBB>
<XXX> <XXX>
<DDD> <DDD>
<EEE/> <EEE/>
<DDD/> <DDD/>
<CCC/> <CCC/>
<FFF/> <FFF/>
<FFF> <FFF>
<GGG/> <GGG/>
</FFF> </FFF>
</DDD> </DDD>
</XXX> </XXX>
<CCC> <CCC>
<DDD/> <DDD/>
</CCC> </CCC>
</AAA> </AAA>
OTHERS
Intro The div operator performs floating-point division
The mod operator returns the remainder from a truncating division.
The floor function returns the largest (closest to positive infinity) number that is not greater than the argument and that is an
integer.
The ceiling function returns the smallest (closest to negative infinity) number that is not less than the argument and that is an
integer.
XPath //BBB[position() mod 2 =0]
//BBB[ position() = floor(last() div 2 + 0.5) //CCC[ position() = floor(last() div 2 + 0.5) or
or position() = ceiling(last() div 2 + 0.5) ] position() = ceiling(last() div 2 + 0.5) ]
Target Select even BBB elements Select middle BBB element(s) Select middle CCC element(s)
Results
<AAA> <AAA> <AAA>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<BBB/> <BBB/> <BBB/>
<CCC/> <CCC/> <CCC/>
<CCC/> <CCC/> <CCC/>
<CCC/> <CCC/> <CCC/>
</AAA> </AAA> </AAA>
• What is XPath?
• What is generic syntax of XPath
selector?
• How many XPath locators are
there? What are they?
Practice 1 • How many types of XPath? What
Now please answer questions to are they?
summarize things about XPath selector
you have just learned • What is XPath axis? List out at least
5 axes in XPath.
• How to mention an attribute in
XPath selector?
• List out 5 XPath functions you have
learned?
• TC: Verify that user is able to
create new tours in PHP travel
admin
1. Login to Admin site
2. Click on Account tab and verify
Practice 2 login correct or not
3. Back to dashboard page
So that we need to define all elements xpath
locators that we will execute actions on them 4. Click on tours menu
to go through the flow.
5. Click on tours sub menu
User: https://www.phptravels.net/
Email [email protected] 6. Click add button
Password demouser
Admin: https://www.phptravels.net/admin
7. Fill required information
Email [email protected]
Password demoadmin
8. Click submit
Expected result: The new tour
should be created
• Go through user site and create
any e2e test case for any
function
• Then catch the elements for
your test cases.
Practice 3
So that we need to define all elements xpath
locators that we will execute actions on them
to go through the flow.
User: https://www.phptravels.net/
Email [email protected]
Password demouserAdmin:
Admin: https://www.phptravels.net/admin
Email [email protected]
Password demoadmin