XPATHS IN SELENIUM


XPATH :-  It is a query language for selecting nodes from an XML document. It is use to identifying the element in the web page. In simple words, finding address of an element on the web page, you can use so many ways to identify the element using XPATH.

In XML document starting node is called as root node or context node.


Xpath in selenium can be categorized in two parts.

1. Complete or Absolute XPATH
2. Partial or Relative XPATH

> Firepath can be used to find out the complete and partial xpath.

Complete or Absolute XPATH
Complete xpath always start from the root node i.e. <html> tag of the document, however complete xpath is not recommended since if any division or developer change any thing in between then xpath will change thus it will be risky to work with complete xpath.

Partial XPATH

  • Partial Xpath will always start with \\tagname[@attribute = 'value']
  • It start with // (double slash).
  • we can use replace tagname with a (*) star so that it will search for all the element.
  • However suing (*) star in partial xpath is not recommended.



Note:- 
In partial xpath there will be a .(dot), don't really need to use this.
Firebug always try to create partial xpath using ID attribute.
Suppose we investigate any web element which does not building with the help of ID, then firebug will provide us complete xpath
We can create our own xpath by providing attribute which ever we want.



CUSTOM XPATH 

We can below methods to identify the element.

  • Single Attribute
    • //tagname[@attribute = 'value']
  • multiple Attribute
    • //tagname[@attribute = 'value' and @attribute1 = 'value']
    • //tagname[@attribute = 'value' ][ @attribute1 = 'value']
  • Contains
    • //tagname[contains(@attribute,'partialAttributevalue/fullAttributeValue')] 
    • Example :-.//*[contains(@class,'inputtext _55r1 inputtext _1kbt inputtext _1kbt')]

  • Starts-With
    • //tagname[starts-with(@attribute,'partialAttributevalue/fullAttributeValue')] 
    • Example :-.//*[starts-with(@class,'inputtext _55r1 inputtext _1kbt inputtext _1kbt')]

  • Text
    • \\tagname(text()='fullTextValue']
    • .//*[text()='Forgotten account?']
  • Ancestor
  • Preceding Node
  • Descendant
  • Following Node








Post a Comment