What is an XPath:
XPath can be viewed as a way to navigate round XML documents.
Absolute XPath:
Absolute XPath starts with the root node or a forward slash (/).The advantage of using absolute is, it identifies the element very fast.Disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.
Relative Xpath:
A relative xpath is one where the path starts from the node of our choice - it doesn't need to start from the root node.It starts with Double forward slash(//)
Syntax://table/tbody/tr/th
Advantage of using relative xpath is, you don't need to mention the long xpath, you can start from the middle or in between.
Disadvantage here is,
i)it will take more time in identifying the element as we specify the partial path not (exact path).
ii)If there are multiple elements for the same path, it will select the first element that is identified
XPath Axes :
XPath has a total of 13 different axes, which we will look below. An XPath axis tells the XPath processor which “direction” to head in as it navigates around the hierarchical tree of nodes.
self:Which contains only the context node
Self returns the current node.
//*[@class='footer']/self::*
ancestor:contains the ancestors of the context node, that is, the parent of the context node, its parent, etc., if it has one.
//div/a[text()='xyz']/ancestor::div[@id='abc']
ancestor-or-self:contains the context node and its ancestors
//div/a[text()='xyz']/ancestor-or-self::div[@name='x']
attribute:contains all the attribute nodes, if any, of the context node
xpath=//body/div[3]/form/fieldset/input[@type='search'] where type is an attribute
child:contains the children of the context node
xpath=//body/*[3] contains 3rd child element of body
descendant:contains the children of the context node, the children of those children, etc.
//td[contains(text(),'xyz')]/following-sibling::td[1]/descendant::div
descendant-or-self:contains the context node and its descendants
//td[contains(text(),'xyz')]/following-sibling::td[1]/descendant-or-self::div
following:contains all nodes which occur after the context node, in document order
//input[@id='email']/following::*
following-sibling:Selects all siblings after the current node
//td[contains(text(),'xyz')]/following-sibling::td[1]
namespace:contains all the namespace nodes, if any, of the context node
parent:Contains the parent of the context node if it has one
//input[@id='email']/parent::*
preceding:contains all nodes which occur before the context node, in document order
//input[@id='pass']/preceding::tr
preceding-sibling:contains the preceding siblings of the context node
//select[@id='day']/preceding-sibling::select/
XPath can be viewed as a way to navigate round XML documents.
Absolute XPath:
Absolute XPath starts with the root node or a forward slash (/).The advantage of using absolute is, it identifies the element very fast.Disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.
Relative Xpath:
A relative xpath is one where the path starts from the node of our choice - it doesn't need to start from the root node.It starts with Double forward slash(//)
Syntax://table/tbody/tr/th
Advantage of using relative xpath is, you don't need to mention the long xpath, you can start from the middle or in between.
Disadvantage here is,
i)it will take more time in identifying the element as we specify the partial path not (exact path).
ii)If there are multiple elements for the same path, it will select the first element that is identified
XPath Axes :
XPath has a total of 13 different axes, which we will look below. An XPath axis tells the XPath processor which “direction” to head in as it navigates around the hierarchical tree of nodes.
self:Which contains only the context node
Self returns the current node.
//*[@class='footer']/self::*
ancestor:contains the ancestors of the context node, that is, the parent of the context node, its parent, etc., if it has one.
//div/a[text()='xyz']/ancestor::div[@id='abc']
ancestor-or-self:contains the context node and its ancestors
//div/a[text()='xyz']/ancestor-or-self::div[@name='x']
attribute:contains all the attribute nodes, if any, of the context node
xpath=//body/div[3]/form/fieldset/input[@type='search'] where type is an attribute
child:contains the children of the context node
xpath=//body/*[3] contains 3rd child element of body
descendant:contains the children of the context node, the children of those children, etc.
//td[contains(text(),'xyz')]/following-sibling::td[1]/descendant::div
descendant-or-self:contains the context node and its descendants
//td[contains(text(),'xyz')]/following-sibling::td[1]/descendant-or-self::div
following:contains all nodes which occur after the context node, in document order
//input[@id='email']/following::*
following-sibling:Selects all siblings after the current node
//td[contains(text(),'xyz')]/following-sibling::td[1]
namespace:contains all the namespace nodes, if any, of the context node
parent:Contains the parent of the context node if it has one
//input[@id='email']/parent::*
preceding:contains all nodes which occur before the context node, in document order
//input[@id='pass']/preceding::tr
preceding-sibling:contains the preceding siblings of the context node
//select[@id='day']/preceding-sibling::select/