In Selenium test automation, if webobject properties does not have id, name, class then we left with 2 options-
Option 1 calculate xpath
Option 2 Calculate XssSelector
In this post, we will calculate about the xpath and various XPath expression for webobjects whose attributed are changing dynamically.
There are 2 types of xpath method
1- Absolute xpath- It is not recomended, as its not optimized and contains full path from a top node which is unique. It increases failure chances as a lot of dependencies are there.
Syntax example- html/body/div/div/div/div/div/div/div/div/div/div/div/div/a
2- Relative xpath- Most preferred, As it is optimized and lesser chances of failure are there, its represented something like-
Relative xpath: //*[@class=’tst-Search’]/td
//– Current node
tagname– Every html web object is tagged to tag which is div, image, input, button etc
@– Attribute selection
Attribute – Name of attribute like class, type etc
Value– Value of attibute- given by html designers.
Ther are couple of tools to find xpath-
1- firebug– in firefox browser
2- firepath– in firefox browser
3- chropath– in chrome browser
We will see how to find xpath in chrome browser-
1- Click on top right and then select more tools, in your chrome browser, as shown in below image-
Select on Control + ‘F’ using keyboard keys, you will get highlighted text where xpath can be written and validated. Chrome browser highlights the correct result in User Interface.
Text contains string ‘Value’
Weblink contains values in single quote
Using start with opetor
Using OR as a logical operator when there are 2 values can be of a web object
Xpath=//*[@type’Vale’ OR @name=’val2′]
Using AND as a logical operator when there are 2 values can be of a web object
Xpath=//*[@type’Vale’ AND @name=’val2′]
Text contains value which is inside td
Axes method- Follwoing- Xpath=//*[@type=’Value’]//following::input
Axes method- Ancester – Xpath=//*[text()=’Value’]//ancestor::div
Xpath is one way of finding a element whose attributes valueis chaning or id, class and name is not available.
Xpath is of 2 types- Absolute and Relative.
Xpath usually work fine however in Internet Explorer sometime it didnot work as Internet Explorer has its ownxpath engine which understand bit different than Chrome and Firefox therefore for cross browser automation scritps it is recomended to use CssSelector, My next post is on CssSelector.