Types of XPath:
X-Path is address of an element in web page source code
X-Path is two types such as :1. Absolute X-path 2. Relative X-path
Absolute x-path is an address of an element by forming tags from html to target element tag. Here XPath expression is created using the selection from the root node. It starts with a single slash ‘/’ and traverses from the root to the whole DOM to reach to the desired element
For Eg:
/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]
In the above XPath example in Selenium, if any tag name like section or one of the div’s changes the whole XPath would become invalid leading to script failure.
Relative XPath , the XPath expression is generated from the middle of the DOM structure. It is represented by a double slash ‘//’ denoting the current node. In this case the search will start from the mentioned tagname and string value. It is more compact, easy to use and less prone to been broken.
For Eg: //input[@name=’email’]
In above XPath example in Selenium, we are searching from the current node with tagname input having attribute as name with value as email.
Writing Dynamic XPath In Selenium Through Various Methods:
Syntax1: //tagname[@attribute=’value’]
Syntax2: //*[@attribute=’value’]
Here * means any tag name in XPath concepts in selenium
Syntax3: //*[@attribute=’value’] [@attribute=’value’]
To find an element when both conditions are true
Syntax4: //*[@attribute=’value’ OR @attribute=’value’]
To find an element when any one conditions is true
Syntax5: (//*[@attribute=’value’])[INDEX]
Syntax6: //*[contains (@attribute, ‘partial text’)]
Syntax7: //*[starts-with (@attribute, ‘starting text’)]
Syntax8: (//*[@attribute=’value’])[last()] or (//*[@attribute=’value’])[last ()-1]
Syntax9: //*[@attribute=’value’]/div/div/input
Above XPath is the combination of Absolute XPath and Relative Xpath
Syntax10: //*[@attribute=’value’]/parent::*
(or)
//*[@attribute=’value’]/parent::tag name
Syntax11:
//*[@attribute=’value’]/child::*[index]
(or)
//*[@attribute=’value’]/child::tagname [index]
Syntax12:
//*[@attribute=’value’]/preceding-sibling::*[index]
(or)
//*[@attribute=’value’]/preceding-sibling::tagname[index]
Syntax13:
//*[@attribute=’value’]/following –sibling::*[index] (or)
//*[@attribute=’value’]/following-sibling::tagname[index]
Syntax14:
//*[@attribute=’value’]/ancestor::*[index]
(or)
//*[@attribute=’value’ ]/ancestor::tagname[index]
Syntax15:
//*[@attribute=’value’]/ descendant::*[index]
(or)
//*[@attribute=’value’ ]/ descendant::tagname [index]
Syntax16:
//*[@attribute=’value’]/preceding::*[index]
(or)
//*[@attribute=’value’ ]/preceding::tagname[index]
Syntax17:
//*[@attribute=’value’]/following::*[index]
(or)
//*[@attribute=’value’ ]/following::tagname[index]
Note: From the Relative XPath syntax’s ,XPath concept is working as a language it have symbols, methods and axes
XPath Symbols :
[]
()
,
@
=
*
//
/
Or
-
::
XPath Methods :
text(),
contains(),
last(),
starts-with
XPath Axes :
parent ,child, ancestor, descendant, following, following-sibling, preceding, preceding-sibling