Types of XPath

  1. Absolute XPath

Absolute XPaths starts with the root of the HTML pages.

Absolute XPaths are not advisable for most of the time due to following reasons

  1. Absolute XPaths are lengthier and hence they are not readable
  2. Absolute XPaths are brittle when minor structural changes are done to the web pages

Absolute XPaths shall be used only when a relative XPath cannot be constructed. (highly unlikely). Absolute XPaths tends to break as web pages/content is changed. Hence it is not recommended to use absolute XPath in Selenium.

Syntax: Absolute XPaths start with  /html

Example: /html/body/div[1]/div/div[2]/form/div[2]/input

2. Relative XPath

Relative XPaths is used for locating an element with respect to an element known as XPath. The element of your choice is referred relative to a known element.

Syntax: Relative XPaths are started with two forward slashes ‘//’.

Example: //div[@id=’divUsername’]/input

Note: Absolute XPaths are faster than the relative XPaths

3. Exact XPath

Locating elements using their attributes, values and inner text of the elements.