XPath stands for XML Path Language (don’t ask me how, but luckily it’s not that important to this post). It’s a query like language made to easily navigate XML. Let’s look at a simple example.
<book> <title>Agile Conversations</title> <author>Douglas Squirrel and Jeffrey Frederick</author> <price>19.95</price> </book>
Then you can use XPath to select the ‘Author’ node (or any other node). If you wanted to grab the author node, you would do it like this:
In fact, it would select all author nodes, if there was more that one. But how does this help our automation? After all, we rarely automate XML. The thing is that HTML and XML is the same structure. And that means that XPath also works on HTML. So we can also use it to find nodes in the Document Object Model (DOM). So instead of finding the author, maybe we find a specific text or button. This, as it turns out, can be very powerful.
Of course for this to work, your tool needs to support XPath. I have only worked with Selenium and Blue Prism. Both of these supports XPath. And in both cases, it’s often the best way to find nodes (sometimes also referred to as elements).
Let’s look at a simple HTML example instead (I have omitted some things, like the head tag, for simplicity).
<html> <body> <div class="title">Select an option</div> <button class="option-button">Option 1</div> <button class="option-button">Option 2</div> <button class="cancel-button">Cancel</div> </body> </html>
Let’s say we want to select the button with the text “Option 2”. There are multiple ways to do this, but the most robust is likely to include the type (button), the class (option-button) and the text (Option 2). In XPath it would like like this.
//button[@class='option-button' and contains(text(), 'Option 2')]
While it does take a bit of getting used to, XPath is a very concise way of describing nodes. You could have done the same in Blue Prisms application modeller, without XPath. But it would have required three different properties to match on.
And once you reach the limits of what you can do with the other tools (like the Blue Prism application modeller), XPath has almost no real limits. Anything that can be described uniquely in some way, can be selected with XPath. It even allows matching on some of the less common attributes, like aria-labels.
I’m in – now what?
As seen in the example, simply trying to guess the syntax of XPath isn’t very viable. Luckily there are plenty of resources out there. To get started you might want to read an XPath Tutorial – like this one from W3Schools. Another favourite of mine is this XPath cheatsheet. And finally, once you start working with specific needs, you can always use Google. For instance you could search for “XPath contains text and class” to find out how to use more that once criteria. Once you think you have the syntax, you can use tools like XPather to test it.