This article is part of a series of articles covering Selenium and various web technologies for becoming more proficient in Test Automation.
In module 5, we introduced Web Technologies. If you remember well, HTML had a variant called XHTML because it was more compliant to XML.
In this module, we are introducing XPath which is a technology for navigating through XML documents. Because of the strong resemblance of HTML/XHTML, it can also be used for navigating those.
XML Path Language or XPath current version is 3.1 and is defined by the W3C.
Selenium and many other automation tools use XPath to specify the UI control of interest.
XPath provides a few node selectors listed in the table below:
The root node is the top-level node that is not visible in a document and has no name. In Figure 2: HTML Code Example, it corresponds to line 1 and is represented by /. In the table below, we list a few examples:
Using a node selector, you can select a node by giving the absolute path or part of the part. Note that multiple nodes may match the selector. Lastly, a selector can end with @attrName which indicates that the matching attributes are selected, not the nodes.
A predicate can be used to refine the node(s) we want to match:
The available op operations are =, !=, <, >, <= and >=.
The following table shows the matches from the previous example using an XPath notation:
When using the Firefox or Chrome browser, you can display the developer toolbar by pressing the F12 key.
This tool allows you to inspect the currently displayed HTML page. Once the developer toolbar is displayed, click in the Elements section and press Ctrl+F to perform a search. A search bar will be displayed right at the bottom of the toolbar:
You can start typing an XPath value to perform a search. The first match will be displayed. If more than one match is available, use the arrows on the right-hand side to see the other matches!