Absolute Relative XPath in Selenium:

 

What is XPath:

  • It helps to detect any element on any web page that uses the conventional XML path expression.
  • XPath uses HTML DOM structure that looks like XML path expression.

 

Syntax for XPath:

 

  • // : Used to select the current node.
  • Tag_name: Name of the tag of a particular node.
  • @: Used to select to select attribute.
  • Attribute_name: Name of the attribute of the node.
  • Value: Value of the attribute.

 

1. Absolute XPath:

  • Absolute XPath starts with the root node.
  • It is also called complete or full XPath.
  • Absolute XPath starts from <html> tag.
  • it starts from single slash(/).

 

Example:

 

Choosing Absolute xpath using FirePath:

2. Relative Xpath:

  • A relative xpath is one where the path starts from the node of your choice.
  • it doesn’t need to start from the root node.
  • It starts with Double forward slash(//).

 

Example:

 

Choosing Relative xpath using FirePath:

 

3.  Difference between single ‘/’ or double ‘//’:

“/”:

  • It starts selection from the document node.
  • It Allows you to create ‘absolute’ path expressions.

Example: “/html/body/a” matches all the a tag elements.

 

“ //”:

  • It starts selection matching anywhere in the document.
  • It Allows you to create ‘relative’ path expressions.

Example: “//a” matches all the a tag elements.

 

4. Handle Complex & Dynamic elements using XPath in Selenium:

 

a. Basic XPath

  • XPath can be select nodes or list of nodes on the basis of attributes like ID , Name, Classname, LinkText etc.

 

b. Contains()

  • Contains() is used when the value of any WebElement changes dynamically, for example, login Page ID’s.

 

c. Using OR & AND

  • OR in the expression, two situations are used, whether the first condition OR the second position is correct.
  • It also applies if one condition is true or may be both. This means that to find the element one condition must be correct.
  • Under XPath expression, it identifies elements whose one or both conditions are true

 

d. Text()

  • Find the element with exact text match.

 

Example of Absolute Relative xpath in Selenium:

What is the difference between absolute and relative xpaths and Different ways of choosing xpath in Selenium WebDriver