One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.
Absolute XPaths contain the location of all elements from the root (html) and as a result are likely to fail with only the slightest adjustment to the application. By finding a nearby element with an id or name attribute (ideally a parent element) you can locate your target element based on the relationship. This is much less likely to change and can make your tests more robust.
Since only xpath locators start with “//”, it is not necessary to include the xpath= label when specifying an XPath locator.
1
2
3
4
5
6
7
8
9
10
* xpath=/html/body/form[1] (3) – Absolute path (would break if the HTML was changed only slightly)
* //form[1] (3) – First form element in the HTML
* xpath=//form[@id='loginForm'] (3) – The form element with @id of ‘loginForm’
* xpath=//form[input/\@name='username'] (4) – First form element with an input child element with @name of ‘username’
* //input[@name='username'] (4) – First input element with @name of ‘username’
* //form[@id='loginForm']/input[1] (4) – First input child element of the form element with @id of ‘loginForm’
* //input[@name='continue'][@type='button'] (7) – Input with @name ‘continue’ and @type of ‘button’
* //form[@id='loginForm']/input[4] (7) – Fourth input child element of the form element with @id of ‘loginForm’
* Expresion in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection.
/AAA/BBB[1]
Select the first BBB child of element AAA
/AAA/BBB[last()]
Select the last BBB child of element AAA
Attributes are specified by @ prefix.
//@id
Select all attributes @id
//BBB[@id]
Select BBB elements which have attribute id
//BBB[@name]
Select BBB elements which have attribute name
//BBB[@*]
Select BBB elements which have any attribute
//BBB[not(@*)]
Select BBB elements without an attribute
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.
//BBB[@id='b1']
Select BBB elements which have attribute id with value b1
//BBB[normalize-space(@name)='bbb']
Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison
Function count() counts the number of selected elements
//*[count(BBB)=2]
Select elements which have two children BBB
//*[count(*)=2]
Select elements which have 2 children
Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.
//*[name()='BBB']
Select all elements with name BBB, equivalent with //BBB
//*[starts-with(name(),'B')]
Select all elements name of which starts with letter B
//*[contains(name(),'C')]
Select all elements name of which contain letter C
< BBB />
< BBB />
< BBB />
< BBB />
The string-length function returns the number of characters in the string. You must use < as a substitute for < and > as a substitute for > .
//*[string-length(name()) = 3]
Select elements with three-letter name
Several paths can be combined with | separator.
//CCC | //BBB
Select all elements CCC and BBB
/AAA/EEE | //BBB
Select all elements BBB and elements EEE which are children of root element AAA
/AAA/EEE | //DDD/CCC | /AAA | //BBB
Number of combinations is not restricted
No comments:
Post a Comment