LambdaTest Community

Find answers, support, and inspiration from other users

Questions 2
LambdaTest Community
Answered

What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?

Can anyone please guide me upon the diffence between relative and abolute xpaths and also tell me which is pefered in selenium automation testing.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers 2


Absolute XPath: The XPath starts Root node with following a hierarchy to the element with corresponding nodes from the page. nodes path represent like ‘/’.

Eg : html/body/div[2]/div/input

Note: absolute XPath is not recommended for better element location If any add/change in the nodes the hierarchy should be changed and we can get the proper location for an element.

Relative XPath: The XPath should construct with particular element related attributes(element characteristic). it represents like “//”.

Eg: //input[@id=‘email’]

And Finally many plugins available to get proper XPaths.

Eg: firepath, Testsigma(chrome)

Testsigma Chrome extension will give very unique and simplest paths.

Up Vote Down Vote 1 Votes
Posted 4 months ago

In Selenium, XPath is nothing but the XML Path. It helps in locating any element on a web page that uses traditional XML path expression. XPath uses HTML DOM structure that looks like XML path expression.

Xpath are of two types as follows:

  • Absolute Xpath: It is the most straight forward way of locating a web element but the demerit for the absolute Xpath is that it gets failed when there are any changes made in the Xpath of the web element on the HTML document. It has a characteristic that it begins with a single forward slash character (‘/’) i.e. selecting an element starting from the root node. Following is the syntax cum example for an absolute XPath expression of the element:

html/body/div[1]/section/div[1]/div/div/div[3]/div/div/div/div/div[2]/div[1]/div/h3[2]/b

  • Relative Xpath: For this type of Xpath, the path begins from the middle of the HTML DOM (Document Object model) structure. It has a characteristic that it begins with a double forward slash character (‘//’) i.e. selecting a web element from anywhere of the web page. It has a benefit to begin from the middle of the HTML DOM structure and abstain from writing long XPath starting from the root node. Following is the syntax cum example for a relative XPath expression of the element:

Relative xpath: //[@class=’class-test-box’]//

Have a look at this link for more Automation details.

Up Vote Down Vote 0 Votes
Posted 4 months ago