XPATH in Selenium

XPATH is a way to identify the element by navigating the entire html source code and identify the element using back-end attribute.

Syntax of XPATH

// To go to entire html source code

/   Navigating from parent to child

[ ]  Attribute Declaration

[ ]   Navigating from child to parent

@   Attribute annonation

We can give the xpath of an element either by absolute or relative.

Absolute XPATH
Whenever the XPATH is written from parent to child it is known as absolute XPATH. In real time we will not move for absolute XPATH because if the requirement got changed  or location of the element got changed it will fail to identify the element.

Relative XPATH

Whenever the xpath is written by using back end attribute of the element it is known as relative xpath.

Using one attribute when we will get multiple matching node we will take help of other attribute present in the html source code.

Sometime using attribute it fails to identify the element so we will go and take of parent and first we will identify the parent and then navigate to its child.

Function in XPATH


Syntax- //html tag[text()=’value’]
eg- //h[text()=’username’]

These are the advance function to write the xpath whenever their is no attribute given in html source code only visible text is their we will go for text().


It is used to handle the dynamic element present on the web page.
Generally is used to handle ajax application.
Ajax is a java framework which is used to develop dynamic element.

Syntax// htmltag[contains(test(),’value’)]


With the help of part of a string we will write the XPATH to handle dynamic element.
Contains function automatically remove the space present before the starting and after the string.


It is used to remove the space present before the string and after the string present  in visible test or attribute value and compare the string with string  and return the .It is the supporting function of text function.
eg//td[normalize-space(text())=’Open Task’]

preceding sibling

It is used to identify the previous element of a web element .

When to use the following or preceding sibling

When the element attribute is dynamic and we are not able to identify the element using attribute we go for following or preceding sibling.

following sibling

Following sibling is used to identify the next element of a web element.


We use descendant to skip some html tag.


Ancestor lets you select any ancestors [e.g., Parent and Grandparent] of the current node.


Ancestor-or-self lets you select any ancestors [e.g., Parent and Grandparent] of the current                      node including the current node.


Attribute returns all the attributes in the current node.


Child returns all the children in the current node.


Following returns all in the document after the closing tag of the current node.


Namespace returns all namespace nodes in the current node.


Parent returns the parent of the current node.


Preceding is a reverse of Following;  Preceding returns all in the document before the current node


Self returns the current node.