XPATH LOCATORS AND SELENIUM

XPATH LOCATORS AND SELENIUM

 

A web application developed may not always follow the best practices or testability guidelines. Not every application objects will have a suitable id or name property using which we can identify object uniquely. Here Xpath comes to aid and facilitates us in identifying the element of interest by using either absolute xpath or relative xpath. We have discussed in brief about xpath in our earlier article here. In this article we will in detail understand more about Xpath and some techniques using xpath axes which can help us identify the object of interest.

 Let us first understand what do we mean by Xpath axes and then we will take an example and understand different locator values created.

 

Xpath Axes

As per w3 school definition, xpath axes is an axis which defines a node-set relative to the current node. We need to understand that the node which we take relative generally have a strong identifier, generally id or name is available using which the node is identified, then relative to that node we find our element of interest by forming an axis. The different axis available are as follow s-

AxisName

Result

ancestor

Selects all ancestors (parent, grandparent, etc.) of the current node

child

Selects all children of the current node

descendant

Selects all descendants (children, grandchildren, etc.) of the current node

following

Selects everything in the document after the closing tag of the current node

following-sibling

Selects all siblings after the current node

parent

Selects the parent of the current node

preceding

Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes

preceding-sibling

Selects all siblings before the current node

The above axis defines the different hierarchical relation which can exists between two nodes. In here we will see an example and try to construct different relation between nodes. For example the scenario is as follows , and we will be using firebug to verify locators.

a.       Open 5 Elements learning demosite- http://5elementslearning.com/demosite

b.      Login with registered user credentials

c.       Buy a few products

d.      Go to the view orders page and we will discuss about an order element.  

img

 

Here while trying to read the information of one order, I ensured I traversed uptil a node where the div had an id. Now with relative to that div I will be creating locators. Let us see them.

imglocator

 


The above table defines with example the different xpath axes formed. Besides this we can use other ways to capture Xpath, by using few more types.

 

Contains

Example Xpath=//*[contains(@href,'My Account')], will refer to the link My Account. - Contains() is a method used in XPath expression. It is used when the value of any attribute changes dynamically, for example, login information

Text

//button[text()='Sign In'], this will search for a button where button’s text is Sign in. It is available in the login page of the website.

And / OR

Xpath=//*[@type='submit' AND @id='tdb5'],  The above will also work on the sign in button.

Starts-with

Start with- Xpath=//label[starts-with(@id,'tdb')], Will identify all label tags where id starts with tdb

Similarly there is lot more to xpath then the above article, hope your interest is now generated to refer to it. To learn more about xpath refer to w3 schools website.  

No comments

Leave a reply

Your email is never published nor shared. Required fields are marked *