We have learned to how to identify element[s] by using xpath & css locator strategies, this post will explain the xpath & CSS feature like adding multiple expressions or using multiple attribute identifications in side the XPATh or CSS.
Prerequisite –
Refer locator strategies
Refer xpath & CSS patterns
Refer xpath & CSS methods
Multiple xpath expressions
We can use multiple xpath expressions to identify the elements
Example – we can use name & id attribute both to identify the element
Each of the xpath expressions can be used with or | and
operators
Xpath syntax –
//input[starts-with(@id,'name') or @name='uname']
Above syntax will find the elements when either of the expressions matches, either starts-with(@id,'name')
or @name='uname'
The same way, we can use and operator, where xpath finds the elements only when both of the expressions matches.
Multiple CSS expressions
Like Xpath, CSS allows to use multiple expressions to identify elements.
The operator syntax is bit different then xpath
css and syntax –
#1
input[id='value1'][name='value2']
Matches elements that has both id=value1 & name =value2, if any one of these criteria not matching, then element can not be identified.
#2
input[id$='name'][name='uname']
We already learned the $ depicts the starts-with method, this below syntax will match elements that satisfies the – id that starts with text “name” and name = “uname”
css or syntax
Matches any element that satisfies either of below expressions
input[id$='name'],[name='uname']
Matches elements that has either id starts with text “name” or name attribute is “uname”
1 Comment