Absolute and Relative XPath in Selenium
Introduction
In selenium there are 8 types of locators are used which help to find out the web elements like Name, Id, class name, Tag name, Link text, Partial link text, XPath, CSS selector,so in this tutorial we see what is XPath and absolute and relative XPath.
In this tutorial you learn about the basic concept of selenium.Before you learn the selenium you must know the concept of java so learn about the java visit our java articles.What is java?Different java applications Advantages,disadvantages
Before we start to understand the absolute and relative Xpath first need to understand what is the DOM
What is DOM ?
Full form of DOM is Document Object Model .DOM is used to identify the webelements.DOM is structural representation of HTML structure.
Following are the major steps to find out the elements in DOM
1. Open the application which you wants.
2.Right click on the element which you want.
3.Select the option inspect or you just click the F12 key from keyboard.
Following are the different types of locators are used to find out the web elements in selenium
1.Id
2.Name
3.Class Name
4.Tagname
5.Link Text
6.Partial Link text
7.XPath
8.CSS selector
What is Web Element
Whatever is present on the webpages we called it web elements.These all elements are design by HTML(HyperText Markup Language).
As we know web element is an elements which we see on the our web pages.Web element contains like username,password,login,logout etc this all are web element of login page.
This all web elements are made by HTML code.HTML contains tags attributes and text by using this components design the web components which we see on our different types of web pages.
So,in this tutorial we see the XPath locator and there types.
XPath in Selenium
XPath in selenium is generally used when element is not found by the given locators like id,class,name etc then XPath locator is help to find the webelement.
XPath in selenium is a locator which is used to find the web elements by using XML path.In two way we find out the XPath by using relative XPath and absolute XPath.
Important point in XPath is help to find out the exact of XML and HTML documents.There are two types of Xpath relative and absolute this two types is help to find out the webelement in selenium.
XPath Syntax
Here,
1. // :- current node
2. Tagname :- Take tagname for the specific node
3.@ :- attribute
4.Attribute :- Take attribute for the specific node
5.Value:- Take Value of attribute
Following are the 2 types of XPath
Absolute XPath
Absolute Xpath is first type of Xpath which is start from the element root node to child.Absolute Xpath is know as an simplest path in selenium and this Xpath is starat with only / (single forward slash)
Example
/html/body/div[1]/index - This syntax is for male field
Here,div[1] is one index
<div2> contains both male and female.
Note
In absolute Xpath is works on male element becoz if there is multiple input at time it will give first preference to first element .Here first select the male element and the female.
If we have multiple inputs then use absolute Xpath by index.
Absolute Xpath by Index
Example for female field
html/body/div/input[2]
[2] - It is an index for female
Relative XPath
Relative Xpath is mainly classify into the relative Xpath by index,relative Xpath by attributes, and relative Xpath by text function.Remember that the Relative X path is start with the //(double forward slash).Relative Xpath not need to write the long Xpath also relative Xpath is start from the middle of the HTML structure.
Relative Xpath is find by the following different ways
1.Relative Xpath by index.
2.Relative Xpath by attributes.
3.Relative Xpath by text function.
4.Relative Xpath by contains.
1.Relative Xpath by Index.
In this relative Xpath by index we see the web elements find with the help of index
Syntax = //TagName [index]
Example to better understand relative Xpath by index
E.g In program we write
driver.findelement (By.Xpath ("//input [3]")
sendkey("abc @ gmail.com)
Here, // - current node
input - tagname for the specific node
[3] - index
2.Relative Xpath by Attributes.
In relative Xpath by attributes select the nodes on the basis of different types of attributes like ID,name,class name etc.
Syntax = //tagname [ @attributename = 'attribute value']
Note Below is just example to understand the above syntax use in real time.
Example
driver.findelement (By.Xpath ("//input [@value='signup'])).click();
Here, // - current node
input - tagname for the specific node
@value=signup - Attribute value
Note is Xpath attribute we can pass the multiple attributes by using OR operator
Example
//input [@value='A' or @value='B']
3.Relative Xpath by text function.
We we want to find an element and the emenet is not found by the attribute or contains any attribute but it contains the text value then with the help of this text value we we can identify the Xpath.
Relative Xpath by text is valid to both like absolute and relative Xpath.
Syntax= //tagname[text()='text value']
Example- Here we try to find out the element with forgotten password
driver.findelement (By.Xpath ("//a [text()='forgotten password']).click();
Here, //a - tagname
text() - text
text value-Forgotten password
4.Relative Xpath by contains.
Relative Xpath is used when values of attributes are changes and it is relevant to both relative and absolute Xpath.
Syntax = //tagname [contains(@ attributes name ,'attribute value')]
Example
driver.findelement ("Input [contains@id,'45fgg')].click();
Following are the some special handling characters
Traversing
This type is used to handle the special character here to navigate one element to another element use the axis
Here are we see some axis
1.Child
2.Parent
3.Descendant
4.Ancestor etc.
How to write the Xpath?
Here we take the example for our own website www.learnsoftest.com
1.First right click on the element you are trying to find the Xpath
2.Then click on the inspect
3.Then you see the DOM that is document object model page.
4.After that press F12 or ctrl+F
5.After that you see a search box type in that type Xpath if element is found they see in yellow color.
6.Or you can just right click on the highlighted area on HTML DOM page and just copy the path
7.After this you will get absolute path
8.But to make it relative Xpath you have to makes changes.
Difference between Absolute and Relative XPath in Selenium
Absolute XPath
1.In absolute Xpath is an complete path from start to root of the element.
2.Absolute Xpath is start with the / (single forward slash )slash only
3.Absolute Xpath is lengthy
4.Absolute Xpath time consuming.
Relative XPath in Selenium
1.In Relative Xpath is start from the referring element not from root node.
2.Relative Xpath is start with the // (double forward slash )slash only.
3.Absolute Xpath is not lengthy as compared to absolute Xpath.
4.Absolute Xpath take short time.