Create Absolute and Relative Xpath in Selenium C#

In the previous tutorial, we have seen what is a node and what is Xpath, and how to customize the Xpath. Now we will see what are the basic Xpath functions, the use of the logical operators in the creation of the Xpath, and what are the type of Xpath.

How to use Logical operator’s to create Xpath

Logical operators are generally used in programming for evaluating the two conditions in which we want the conclusion based on the conditions either they both are true or they both are false. In this case, we use three types of logical operators are -And, Or, Equal.

And Operator in Xpath

If we want to choose any node by Xpath only if the given conditions are true so in this case, we use logical and operator. We can give any no. of conditions followed by and keyword.


//<Nodename>[@att1='value' and @att2='value' and ....]

For example- if we want to choose any input node only when it has the attribute id=’email’ and attribute class=’inputtext’ so we will use here the logical and operator just by writing ‘and’ in between the Xpath Selecting node condition.

//Input[@id='email' and @class='inputtext']

This Xpath will select the input node only when both conditions are true like only when the id value is email and the class value is input text.

In this example, if any one condition is false then its result will also false means it will not gonna select any input node if both conditions are not satisfied with the true condition.

Or operator in Xpath

The main functionality of OR operator is, it gives the result as true if any one of them from the given conditions is true, it doesn’t matter that all conditions are true or not.

So if we want to choose any node by Xpath by giving the conditions in which any one of the conditions is satisfying so it will give the result as true.


//<Nodename>[@att1='value' or @att2='value' or...]

We can give any no. of conditions followed by or keyword.

For example- if we want to select an input node that has either value of id= email or the value of class= inputtext so we use here logical or operator. If any one condition of them is true so it will select that input node.

//Input[@id='email' or @class='inputtext']

Equal operator in Xpath

This operator is used to select the currosponding node only when the given condition is equal to it which means the given attribute value is equal to that attribute.



For example- if we want to select the input node which has a type attribute with the value= text so we write it like this.


Functions for customising Xpath

There are also some Xpath functions that are helpful for us while constructing the Xpath.

Contains ()

The functionality of contains method is it will look particular substring for any particular attribute if it contains the same substring value of that attribute then it will return the corresponding node otherwise not.




//Input[contains (@id,'em')]

For this, it will return the all input node which has ’em’ substring in their Id attribute.


The functionality of this is selecting the node which has some string between opening and closing of tag.


<label for="pass">password</label>

We can also use this function for filtering. This will equate the text function for the label node which has the same string between the opening and closing of it.


//label[text()='keep me logged in']


There is a method last which returns us a total no. nodes that are present or we can say the index of the last immediate node or child node.



This will return the second last immediate node of the input node.


It is a function that is used for finding the web element whose attribute value gets changed on refresh or by other dynamic operations on the webpage. In this method, the starting text of the attribute is matched with the element whose attribute value changes dynamically. We can also find elements whose attribute value is static (not changed).


Starts-with (<attribute>,value)

This will select the label node in which the opening and closing of the tag start with the ‘ui’ string for the class attribute.


//label[starts-with (@class,'ui')]


This function is used to remove the space from that particular attribute of the node.



For example- if we have a value of attribute @id=’  abcd ‘ so when we use the normalize function it will return us a value of the id attribute as abcd it will remove all the space before or after from the string but it will not remove the space between the string.



These are some basic functions which more commonly used while constructing the Xpath.

Basic Types of Xpath

There are two types of Xpath generally we can create that are absolute Xpath and relative Xpath.

Absolute Xpath:

The absolute XPath has the complete path starting from the root to that element which we want to identify. It starts with a single backslash (/ )symbol and it’s lengthy also which makes it difficult to understand.

One drawback with the absolute XPath is that if there are any changes made between attributes starting from the root to the target element, our absolute XPath will become invalid.



Relative Xpath:

The relative XPath can be started by referring to the element that we want to identify not important, to begin with, the root node. A relative XPath starts with the double backslash ( // )symbol. It’s mainly used for automation.

Relative Xpath is very short and easy to use and understand.


//div[@class='featured-box cloumnsize1']//h4[1]//b[1]