XPath Technology

This article is part of a series of articles covering Selenium and various web technologies for becoming more proficient in Test Automation.

Introduction

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.

Node Selector

XPath provides a few node selectors listed in the table below:

Figure 1: Node Selectors
Figure 2: HTML Code Example

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:

Figure 3: Node Selection 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.

Predicate

A predicate can be used to refine the node(s) we want to match:

Figure 4: Predicates

The available op operations are =, !=, <, >, <= and >=.

Figure 5: HTML Example 2

The following table shows the matches from the previous example using an XPath notation:

Figure 6: XPath matches from the previous example

Developer Toolbar

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!

--

--

--

I have been interested in business and technology since I was about 10. My interest spans across so many fields but I hope you’ll find my writing useful!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Beta to Help You Create a Data and AI Platform on Your Terms

Fusion Versus DevOps — Why the World of Digital Transformation Will Change in 2021

Anonymous Tryhackme Writeup

Anomaly Detection and Remediation

Transparent title bar on macOS with Flutter

Git Setup

C : source , execution , basic , and extended character sets

Step by step guide for using AWS Cognito, the right way ! — PART III

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Valéry Raulet

Valéry Raulet

I have been interested in business and technology since I was about 10. My interest spans across so many fields but I hope you’ll find my writing useful!

More from Medium

Automation Testing Tutorial: A Starter’s Guide With Example

A Complete Guide For Your First TestNG Automation Script

Handling iFrames in Selenium Based Test Automation

HOW TO IMPLEMENT BDD CUCUMBER FRAMEWORK?