In this and upcoming articles, I am going to explain XPath Expression Locators in detailed.
To get started, I will start explaining the below topic.
What is an XPath?
XPath is a technical language that can be used or invented for traversing/navigating through an XML document.
Sample XML document content looks like below:
<heading>Selenium Test Automation Tutorial</heading>
<body>Selenium Test Automation Tutorial Contents</body>
In order to traverse through the above XML content, the XPath language was created and used for.
The purpose of XML is to communicate/transfer the details between two different machines. To retrieve the data stored or transferred via XML document, the XPath language can be used to traverse/navigate through the XML document to the required node where data is stored.
Example: To retrieve the heading data stored in the above XML content, the XPath language can be used to traverse/navigate to the required heading tags.
Hence XPath is nothing but an XML Path Language.
Hoping that you understand XPath, let’s understand more about XPath by going through the below content.
XPath and HTML Traversing
Since the structure of HTML is similar to the structure of XML, we can use the same XPath language for traversing/navigate through the HTML content.
Sample HTML document looks like below:
<title>Basic Web Page Title</title>
<p id="para1" class="main">A paragraph of text</p>
<p id="para2" class="sub">Another paragraph of text</p>
As the above HTML content looks similar to XML, we can use the same XPath language for traversing/navigating through the HTML content.
We will be using XPath in Selenium, for traversing/navigating through the HTML Web Pages.
XPath Path Expressions
Using XPath language, we can create Path Expressions using which we can traverse/navigate through the HTML document.
Path Expressions in XPath look similar to the File Path.
Example for Filepath: C:\Users\arunm\Pictures
Example of XPath Path Expression: /html/body/ul/li/a
The above XPath Path Expression can be used to traverse/navigate through the HTML code.
Hence XPath language can be used as a query language for traversing and selecting the required tags/information in HTML code using the XPath Path Expressions.
Note: Only understand the above example of XPath Path Expression at a high level.
A detailed explanation of creating XPath Path Expressions manually will be explained in the upcoming articles. I will be explaining the building of different XPath Path Expressions in a step by step manner in the upcoming articles.
- > To learn more about Selenium, continue to the next post (Click on Next Post link below)
- > Check complete Selenium Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning 🙂
On a mission to contribute to the Software Testing Community in all possible ways.