XPath is the language used for locating nodes in an XML document.
As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications.
XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page.
As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications.
XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page.
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.
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.
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 | name="username" type="text" /> name="password" type="password" /> name="continue" type="submit" value="Login" /> name="continue" type="button" value="Clear" /> |
- 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 attribute named ‘id’ and the value ‘loginForm’
- xpath=//form[input/\@name='username'] (4) - First form element with an input child element with attribute named ‘name’ and the value ‘username’
- //input[@name='username'] (4) - First input element with attribute named ‘name’ and the value ‘username’
- //form[@id='loginForm']/input[1] (4) - First input child element of the form element with attribute named ‘id’ and the value ‘loginForm’
- //input[@name='continue'][@type='button'] (7) - Input with attribute named ‘name’ and the value ‘continue’ and attribute named ‘type’ and the value ‘button’
- //form[@id='loginForm']/input[4] (7) - Fourth input child element of the form element with attribute named ‘id’ and value ‘loginForm’
These examples cover some basics, but in order to learn more, the following references are recommended:
- W3Schools XPath Tutorial
- W3C XPath Recommendation
- XPath Tutorial - with interactive examples.
There are also a couple of very useful Firefox Add-ons that can assist in discovering the XPath of an element:
- XPath Checker - suggests XPath and can be used to test XPath results.
More real time examples ...coming soon
No comments:
Post a Comment