What is XPath in Selenium?
Hi Folks! In this post, we will look at What is XPath in Selenium? and How to write XPath in Selenium? This is one of the most frequently asked topics in Selenium Interviews. I have tried to cover XPath from the interview point of view. In case you want to know more about it, you can write to me.
Hi Folks! In this post, we will look at What is XPath in Selenium? and How to write XPath in Selenium? This is one of the most frequently asked topics in Selenium Interviews. I have tried to cover XPath from the interview point of view. In case you want to know more about it, you can write to me.
Also, try: Selenium Quiz with Answers
1) What is
XPath in Selenium?
XPath is a language used for querying XML
documents. It can be used to find web elements inside the webpages. There are
basically two ways by which you can find XPaths and hence there are two types of XPath in Selenium.
1. Absolute XPath
2. Relative Xpath
1. Absolute XPath
2. Relative Xpath
Also, Check
this: Selenium
Interview Questions and Answers
2) What is Absolute XPath in Selenium?
An Absolute XPath is the complete path from the root
element to the element you want to travel. It begins with a single slash(/)
followed by the root element of the DOM(Document Object Model).
Example :
Suppose you want to travel to a <div>
tag inside the body tag
Then,
/html/body/div - would be your Absolute XPath.
3) What is Relative XPath in Selenium?
A Relative XPath is a path that finds the element
directly. It begins with a double slash(//) followed by the element you want to
find.
Example :
//input[type="submit"] - finds a submit button directly.
4) Locating element partially in Selenium - XPath?
contains() can be used to locate elements partially.
Suppose you have the following
tag in HTML :
<input type="text" id="username" />
Then,
//*[contains(@id,"user")] - can be used to locate element partially.
5) Locating element using text in Selenium - XPath?
text() can be used to locate elements using the value of a tag.
Suppose you have the following
tag in HTML :
<p id="user">Dave</p>
Then,
//p[text()='Dave'] - can be used to locate element using text.
6) Moving to parent element in Selenium - XPath?
Consider the following HTML code :
<div>
<p id="child">Hello World!</p>
</div>
Then,
//p[@id='child']/parent::div - can be used to move to parent(div).
Alternatively :
//p[@id='child']/.. - can also be used.
7) Moving to nth-child in Selenium - XPath?
Consider the following HTML code :
<ul>
<li>Item1</li>
<li>Item2</li>
</ul>
Then,
//ul/li[2] - can be used to move to second child which is Item2.
Alternatively :
//ul/li[position()=2]/.. - can also be used.
8) Moving to Sibling in Selenium - XPath?
You can use this scenario when only one sibling is static or only one sibling can be identified uniquely.
Consider the following HTML code :
<ul>
<li>Item1</li>
<li id="item">Item2</li>
<li>Item3</li>
</ul>
Then,
//ul/li[@id='item']/following-sibling::li[1] - selects the first <li> which is Item1.
--------------------------------------------------------------------------------------------------------------
I will be coming up with more
questions in the upcoming posts. Stay Tuned. Thanks.
Do send your comment if any
doubts!

0 Comments