XPath & CSS-Advanced
XPath & CSS-Advanced
CSS selectors
/ Mohammad Monfared
1
1. XPath Basics
● Syntax
● Absolute and Relative
● and / or
● Parents and Children
● Index
● Nested locators
2
Syntax:
Absolute XPATH
/html/body/div[3]/div[2]/form/fieldset/input[9]
Relational XPATH
// input [name=‘Options’]
4
// tag [ condition1 ] # Simple
5
// tag [ condition1 ] [index] #Nth child (of its parent) with this locator
6
2. XPath Functions
● contains() ● string-length()
● starts-with() ● round()
● position() ● floor()
● last() ● not()
● count() ● substring-before()
● normalize-space() ● substring-after()
● translate()
7
contains()
// * [ contains(text(), 'string')]
8
starts-with()
// * [ starts-with(text(), 'string')]
9
position()
// * [ position() = 3] //starts from 1
10
last()
// * [ last()]
11
count()
// tbody [ count (.//tr) = 7 ]
12
normalize-space()
// *[ normalize-space ( text() ) = 'Option1' ]
// *[ normalize-space (@id) = 'Option1' ]
13
translate()
// *[ translate (text(), 'OPTION', 'option') = 'option1' ]
14
Combine normalize-space() and translate()
15
string-length()
// *[ string-length (text()) > 30 ]
// *[ string-length (@id) = 4 ]
16
round()
// td [ text() = '53.76' ]
OR
// td [ round (text()) = '54']
17
floor()
// td [ text() = '53.76' ]
OR
// td [ floor (text()) = '53']
18
not()
// * [ type() = 'radio' and @id='femail']
OR
// * [ type() = 'radio' and not (@id=‘male')]
19
substring-before()
//* [ substring-before ( text () , ':') = 'Password' ]
20
substring-after()
//* [ substring-after ( text () , ':') = 'Password' ]
21
3. XPath Axes
● parent ● following
● ancestor ● following-sibling
● ancestor-or-self ● preceding
● child ● preceding-sibling
● descendent
● descendent-or-self
22
//* [@id=‘abc’] / parent :: *[@id=‘def’] 23
//* [@id=‘abc’] / ancestor :: *[@id=‘def’] 24
//* [@id=‘abc’] / ancestor-or-self :: *[@id=‘def’] 25
//* [@id=‘abc’] / child :: *[@id=‘def’] 26
//* [@id=‘abc’] / descendant :: *[@id=‘def’] 27
//* [@id=‘abc’] / following :: *[@id=‘def’] 28
//* [@id=‘abc’] / following-sibling :: *[@id=‘def’] 29
//* [@id=‘abc’] / preceding :: *[@id=‘def’] 30
//* [@id=‘abc’] / preceding-sibling :: *[@id=‘def’] 31
4. CSS Selectors
32
Advantages
1. CSS selectors typically offer better, faster, and more reliable performance than
XPath in most web browsers.
2. All the JS-based test framework like Cypress use CSS selectors
3. It is necessary to use CSS locators when we are using the JS executor to locate
elements in Selenium/Appium.
Sample expressions:
input[ id = ‘fname’]
button[ value = ‘submit’]
34
ID & Class Selectors
tag # ID-value
input # fname
tag . Class-value
div . tooltip
35
Combination
tag # ID-value [ AttrName = ‘AttrValue’]
tag # ID-value . Class-value
36
Sub-string Match
<input type="checkbox" id="moption“>
Next-sibling (Adjacent)
tag # ID-value+tag # ID-value 38
Pseudo Class - Child
First child
tag # ID-value :first-child
Last child
tag # ID-value :last-child
Select by Index
tag # ID-value :nth-child(index)
Select by Index 39
40
Pseudo Class
<tag>:checked Selects checked <tag> elements (e.g. input)
<tag>:disabled Selects disabled <tag> elements (e.g. button)
<tag>:enabled Selects enabled elements (e.g. button)
<tag>:empty Selects element that has no children
<tag>:hover Selects <tag> elements on mouse hover
<tag>:only-of-type Selects <tag> element that is the only <tag> of its parent
<tag>:only-child Selects <tag> element that is the only child of its parent
<tag>::placeholder Selects <tag> element with the place holder attribute specified
<tag>:invalid Selects all <tag> with invalid value (e.g. input)
<tag>:valid Selects all <tag> with valid value (e.g. input)
<tag>:link Selects all unvisited links
<tag>:visited Selects all visited links 41