Saturday, April 14, 2018

How to handle dynamic xpath in selenium: Xpath Axes

Here we will learn How to handle dynamic xpath in Selenium using Xpath Axes

Xpath locator is use to locate web elements on web pages. It is the path of web element in XML (Extensible Markup Language) file.

Xpath Axes:-

Xpath axes is use to search different nodes from current node in XML file. Mainly Xpath axes is use to handle the dynamic web elements. Some web element's xpath dynamically change on refresh or any other operation. There are few axes methods used in selenium webdriver child, parent, following, follower, ancestor,sibling and preceding etc.

See below which are commonly used in selenium:

Xpath-Axes


Examples:

Child Axes: It defines the children of the current node.

Xpath: //div[@id='irfooterlogo']//child::li

Xpath-Axes-Child




Parent Axes: It defines the parent of the current node.

Xpath: //input[@name='j_captcha']//parent::*

Xpath-Axes-parent




Following Axes: It defines all nodes after current node in same document order.

Xpath: //input[@name='j_username']//following::*

Xpath-Following-Axes




Following-Sibling Axes: It defines all sibling after current node.

Xpath: //input[@name='uri']//following-sibling::input[2] 

Xpath-Following-Sibling-Axes





Preceding Axes: It defines all node before current node in same document order.

Xpath: //a[text()='How to select a value from dropdown']//preceding::li[1]


Xpath-Preceding




Preceding-Sibling Axes: It defines all sibling node before current node.

Xpath: //input[@type='submit']//preceding-sibling::input[2]



xpath-Preceding-Axes




Ancestor Axes: It defines all parents node of current node.

Xpath: //input[@type='submit']//ancestor::tbody[1]


Xpath-Ancestor-Axes




Descendant Axes: It defines all children (Also children of the child node's current node)of the curent node.

Xpath: //div[@id='irfooterlogo']//descendant::li[1]


Xpath-Descendant-Axes





Hope you have learnt well. If there is any confusion, please comment me !!


No comments:

Post a Comment