Hi Guys,
Element locator - XPATH Plus or Minus
XPATH - XML path language > which is having nodes and it attribute elements in XML document. which we can derive values from an XML document.
XPATH which will helps people to write their automation script in easy way.
Generally in any Selenium Testers interview question people never miss to ask this element locator.
Even me too came across this questions many times in my interview.
People Expected answer would be "We should not use XPATH"
But I felt as well as I am doing the element locators is always XPATH for UI automation.
Reason: My DEV or Design Team :) would never give the ID or specific location for all elements in a page. [common scenario will happen in most of the companies]
I felt XPATH allows me to navigate to the entire HTML structure, I can achieve anything compared to other locators.
Once I started to work with Xpath, I felt XPATH writting is difficult for me but I have lots of ways to achieve it.
XPATH is little bit easy to do.
Reason: I used lots of addons which help me to write my automation script faster.
First I will start with addon which I used. Not only below Still lot more in market.
1) Firebug
2) Selenium IDE
3) XPATH checker
4)XPATH Finder
5) XPath tester
6) developer tool[chrome & firefox]
7) xpath helper.
BUT THIS IS NOT ENOUGH.
After these many years one of my friend suggested me the way I am doing is not perfect.
Then I started using different combination things in XPATH.
In most of the blogs I saw only how to write the xpath. So I don't want to explain that.
Below I am planning to give just an highlight of the XPATH possible combinations and its different conditions.
So you need to Search in google and find the real usage of the below combinations.
1) // -> any node it picks
2) //* -> caputres the entire page.
3) /* -> Travels with return full info
4) / -> Which travel on order
5) //div | //div -> or condition
6) //div and //div -> and condition
7) (//div)[1] -> first div condition of array will return.
8) (//div)[1] | (//span)[1] -> returns the combination
9) //a[@id='Jegan'] -> Same element locator with ID
10) //@id -> returns all id element
11) //a[not(@id)] - >which returns the element in anchor tag which does not contains id element.
12) //a[normalize-space(@name)='test'] -> selects the data which has name tag = 'test'
13) //*[count(@id)] -> returns the overall count in the page.
14) //*[starts-with(name(),'j')] -> returns the name tag which starts with letter 'j'
15) //*[contains(class(),'test')] -> returns the class tage which contains text 'test'
16) //*[string-length(name())=1] -> returns the string length count which is equal to 1.
17) //child::jegan -> which is equivalent to //jegan
18) //div/descendant::span -> returns the span element in the whole HTML file
19) //div[@id='jegan']/parent::* -> which returns the parent of the div tag with its id as jegan.
20) //div[@id='jegan']/ancestor::* -> returns the HTML index path except the id tag.
Sibling is child within the single div element.
21) //div[@id='jegan']/following-sibling::* -> returns the next node element tag and the values
22) //div[@id='jegan']/preceding-sibling::* -> returns the previous node element.
23) //div/following::*
24) //div/preceding::*
25) //div/descendant-or-self::*
26) //div/ancestor-or-self::*
27) //div/ancestor::*
28) //div/descendant::*
29) //div/following::*
30) //div/preceding::*
31) //div/self::*
I felt this blog would be helpful for you as well as for me in future.