Finding elements using XPath
XPath (the XML path language) is a query language used to select nodes from an XML document. All the major browsers implement DOM Level 3 XPath (using http://www.w3.org/TR/DOM-Level-3-XPath/) specification, which provides access to a DOM tree.
The XPath language is based on a tree representation of the XML document and provides the ability to navigate around the tree and to select nodes using a variety of criteria.
Selenium WebDriver supports XPath to locate elements using XPath expressions, also known as XPath query.
One of the important differences between XPath and CSS is that, with XPath, we can search elements backwards or forwards in the DOM hierarchy, while CSS works only in a forward direction. This means that using XPath we can locate a parent element using a child element and vice versa.
In this recipe, we will explore some basic XPath queries to locate elements, and then examine some advanced XPath queries.
XML documents are treated as trees of nodes. The...