Importance of Xpath in Automation for identifying web elements

Xpath is known as the navigation through the html sytax of a webpage to identify a particular element

Identifying WebElements:
Any webelement in an application will be assinged with an identifier.
The web elements will have attributes as well as the identifiers listed below
1. ID
2. Name
3. Class

With the above identifiers elements can be identified and can be automated to perform certain set of actions

Reasons to go with Xpath:

1. Some complex applications like AJAX, GWT willdynamically create some identities for the web elements where the
identification becomes difficulty during the automation of an application.

2. In certain scenarios there may be situations where different web elements have same Name and ID even.
where it becomes difficult for any automation tool to perform the actions given.

In above scenarios the alternate solution is "Xpath"

*Xpath is the way of identifying the web elements uniquely.*

Approaches for Xapth:

There are two ways of defining Xpath for web elements, they are listed below
1. Dependency:
 Defining the Xapth by just giving the navigation through the html path to the web elements.

Ex: html/body/table/td[2]/tr[4]

The above appoach is good only if there are no new addition of features in the application, but if any new
features added then the xpath fails here.

2. Non-Dependency/Unique :

Defining the Xapth uniquely which works even if any new web elements added to the application anytime

Ex: //div[text()='Ok']

Some commonly used used commands for writing Xpath for webelemnts:
1. // - Selects nodes in the document from the current node that match the selection no matter where they are
2. / - Selects from the root node
3. * - Matches any element node
4. .. - Selects the parent of the current node
5.@ -   Selects attributes

Tools used for finding Xpath for web elements:

    * Firebug(Add-on for Mozilla browser)
    * IEdeveloper(Addon for IE explorer)
    * Xpather(Add-on for Mozilla browser)
    * XFinder(Add-on for Mozilla browser)

Views: 449

Tags: automation, objectidentification, selenium


You need to be a member of Test Republic to add comments!

Join Test Republic

Comment by Jaya on December 8, 2011 at 5:02pm

Of course XPath locator is one of the most quirky and one of the most precise locator. But this universality is accompanied by the main disadvantage of such locator types. It is slowness. This disadvantage is clearly seen while running the tests under IE while Firefox works with xpath pretty well. This problem is related to internal browser features in particular to the libraries being used for elements location definition. But this is another story. The main thing is that tests which intensively use XPath work extremely slow under IE and this feature is the cause of huge variety of problems related to execution speed as well as quality of the tests itself especially during operations with dynamic content. For this reason CSS locators can be good alternative of XPath.

Comment by Vineela on December 2, 2011 at 7:59pm

Hi Gaurav,

Yes i agree with you.

But learning and implementation of Xpath is more easy than CSS locator.



Comment by Gaurav Khanna on December 2, 2011 at 10:58am

Nice Blog.

However, I have just one question that CSS element locator is better than Xpath or not ? I mean CSS locator is bit fast as compare to Xpath.

© 2014   Created by Test Republic.

Badges  |  Report an Issue  |  Terms of Service