There are two types of xpaths.
1. Absolute.
2. Relative.
Note: The Problem with absolute xpath is, if the structure of the application is changed, then the element won't be located.
How to write Relative xpaths dynamically for selenium:
There is a specific language called "XPATH AXES" by which we can able to write effective xpaths and it is having number of methods to write dynamic xpaths.
List of all Methods supported by XPATH AXES
AxisName | Result |
---|---|
ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node |
ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself |
attribute | Selects all attributes of the current node |
child | Selects all children of the current node |
descendant | Selects all descendants (children, grandchildren, etc.) of the current node |
descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself |
following | Selects everything in the document after the closing tag of the current node |
following-sibling | Selects all siblings after the current node |
namespace | Selects all namespace nodes of the current node |
parent | Selects the parent of the current node |
preceding | Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes |
preceding-sibling | Selects all siblings before the current node |
self | Selects the current node |
Now do a small practice to write effective xpath.
I'm taking Facebook for writing xpaths:
The following image is the source code(firebug) for that Email field of Facebook.
Number of ways to write xpaths to find the email field:
1. //input[@name='email']
2. //input[@id='email']
3. //input[@id='pass']/parent::td/preceding-sibling::td/input[@id='email']
4. //input[@id='pass']/../preceding-sibling::td/input[@id='email']
5. //table[@role='presentation']//input[@id='email']
6. //td[position()=1]/child::input
7. //label[contains(text(),'Email or Phone')]/ancestor::tbody/*/*/input[@id='email']
8. //label[contains(text(),'Password')]/ancestor::tbody/*/*/input[@id='email']
9. //label[text()='Password']/ancestor::tbody/*/*/input[@id='email']
10. //input[@class='inputtext' and @type='email']
11. //label[starts-with(text(),'Password')]/parent::td/parent::tr/following-sibling::tr/child::td/child::input[@type='email']
2. //input[@id='email']
3. //input[@id='pass']/parent::td/preceding-sibling::td/input[@id='email']
4. //input[@id='pass']/../preceding-sibling::td/input[@id='email']
5. //table[@role='presentation']//input[@id='email']
6. //td[position()=1]/child::input
7. //label[contains(text(),'Email or Phone')]/ancestor::tbody/*/*/input[@id='email']
8. //label[contains(text(),'Password')]/ancestor::tbody/*/*/input[@id='email']
9. //label[text()='Password']/ancestor::tbody/*/*/input[@id='email']
10. //input[@class='inputtext' and @type='email']
11. //label[starts-with(text(),'Password')]/parent::td/parent::tr/following-sibling::tr/child::td/child::input[@type='email']
By using above specified ways, you can write the xpaths. You can follow any number of possible ways.
Please like the post if you feel it is useful.