Wednesday, January 28, 2015

Advanced element identification techniques in Xpath

In many cases we can observe that small changes in the application made to the application and previously identified Xpath statements will not work.

we need to get short and least breakable xpath 
Use "combination" of attributes to make the XPath more specific
always use "Relative XPath" instead of "Absolute XPath"

Relative XPath Example: Relative xpath always starts with // 

                   //*[@id='s']
AbsoluteXPathExample: html/body/div/div[4]/div/div[2]/div[1]/div/form/div/input[1]
Use id,name attributes if available
Use XPath functions in XPath



Xpath functions



Position() : by using this function we can locate  for element by html postion
 //input[position()=5] we can locate 5th element

Click here for examples

Last() :This function is for locating last element node of the specific node type such as
//input[last()].

Click here for examples
Further it can be specified as //input[last()-1] to identify an node before the last node

Starts-with(): This is one of function of XPath Language which is used in XPath
Statements to locate the element starting with the specified text or to locate the element node
containing an attribute value which is starting with the specified text.
For Example
//div[starts-with(text(),’irctc’)+ -Matches the element with string which begins with
‘irctc’

//div[starts-with(@id,’selenium’)+ : Matches the element with attribute begins with string ‘selenium’

Contains () :This is one of the predefined methods of XPath Language which is used in XPath
Statement to locate the element node containing specified text on the page or to locate the
element node containing specific text in its attribute value.
For example

//span*contains(text(),’seleniumide’)+:Matches any element with text contains the string
‘seleniumide’

//span*contains(@id,’testing’)] – Matches any element with id attribute containing string 


















No comments:

Post a Comment