What is Xpath and what are the types of it in Selenium WebDriver?

What is Xpath and what are the types of it in Selenium WebDriver?

Hi Brett,

XPath is defined as an XML path. It is a syntax or language for finding any element on the web page using XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure. XPath contains the path of the element situated on the web page. The standard syntax for creating XPath is.

Xpath=//tagname[@attribute='value'] where, // : Select current node.

Tagname: Tagname of the particular node.

@: Select attribute.

Attribute: Attribute the name of the node.

Value: Value of the attribute.

Now, there are 2 types of Xpaths:

  1. Absolute XPath: It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed. For eg. ​

html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b

  1. Relative Xpath: For Relative Xpath the path starts from the middle of the HTML DOM structure. It starts with the double forward slash (//), which means it can search the element anywhere at the webpage. For eg.

//*[@class='featured-box']//*[text()='Testing']