Xpath Optimization

1.Concentrates on how we can identify the objects in an Optimized manner using XPath which are reliable and consistent.

2. There are applications which deal with different kind of objects; not having any id/name(s) and many dynamic objects not having any unique identification.

3. Also, there are lots of technologies integrated with an application such as GWT, DOJO, Flash etc which makes it difficult to develop a consistent code.

Objective

 

Basis of XPath

1. In Selenium automation, the XPath is a very powerful way to parse the HTML code of any web page and find out the XPath path for identifying the elements on the web page.

2. If properly used, it can produce very reliable and low maintenance locators. But when used an inefficient xpath, it can lead to very inconsistent results. XPath is a syntax for defining parts of an XML document. It uses the path expressions to navigate in XML documents.

3. It is like a small programming language, it has functions, expressions, wildcard characters etc. XML documents consist of trees of nodes. The topmost element of the tree is called the root element followed by descendant nodes.

What is a Xpath?

Finding address  of an element on a webpage.

Used to identify element uniquely

 

 

In XML - Starting node root or context node.

 

put xml structure from intellij

 

2 types to identify 

absolute

relative

Absolute :

Starts with start or context node.

Starts with / single slash.

 

Advantage:

Easy , identifies elements fast.

 

Disadvantages:

Cannot recall the element for which webelement.

Once a new element is added or removed the Xpath can breas 

 

Example : html/body/section/div/div/div[1]/form/div/input

Relative:

Starts anywhere in the HTML.

Starts with // double slash.

is comparatively shorter.

 

Disadvantage: Time consuming

Advantage : Chances of Xpath breaking is less

 

 

* shows all the webelements

Axis Name Result
ancestor Selects all ancestors (parent,grandparent, etc.) of the current node
ancestor-or-self Selects all ancestors (parent,grandparent, etc.) of the current node and the current node itself
attribute Selects all attributes of the current node
child Selects all children of the current node
descendant Selects all descendants (children,grandchildren, etc.) of the current node
descendant-or-self Selects all descendants (children,grandchildren, etc.) of the current node and the current node itself
following Selects everything in the document after the closing tag of the current node
following-sibling Selects all siblings after the current node
namespace Selects all namespace nodes of 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
self Selects the current node

Following are the few strategies in order to optimize the XPath:

1. Use id attribute if available

2. Use the combination of attributes to make the XPath more specific

3. Use the Relative XPath instead of Absolute XPath Statements

4. Always avoid using indexes in Xpath.

5. Verify the xpath using Selenium IDE commands

6. Using .. to move to parent of the present node

7. Use XPath functions in XPath wherever necessary to better identification

8. Use Preceding-sibling or Following-sibling wherever applicable 9. Identification of objects with same attribute values 10. Identification of image objects 11. Handling dynamic attribute Values

Example:

 

Use ID attribute if available If the ID attribute is present for an object, use it in XPath even though the object can be identified with any other attributes. ID attribute should have the highest priority.
For example : Based on Example1, It can be used as //tr*@id=’item1’+ @ symbol indicate following is an attribute of the object.

 

Use combination of attributes in XPath If ID attribute is not available, any other attribute which uniquely identifies the object can be used. Also combination of 2 or more attributes can be used to identify the object such as name, type, class, value etc. This will make the XPath more specific. For example, based on Example1, we can have //input*@type=’text’ and @name=’qty’+

In the above example Boolean operator ‘and ‘operator is used in XPath statement to combine more than one attributes. Using combination of properties in XPath is useful in following cases . There are cases where there does not exist any single attribute which uniquely identify the object  Even if there is exist single attribute which uniquely identifies the object, we can use combination of attributes to make Xpath more robust and hence there is less chances of failure in identification

Add-Ons like:

  1. FirePath.
  2. FireBug.
  3. Xpath Helper.
  4. ChroPath. Etc

 

 



 

[contains(text(),'Home')]

text()='SELENIUM'

//child::*/a[@href='demo.html'][contains(text(),'Home')]


//section[@id='wrapper']//header//div[@class='container row']/child::div[@class='span_3_of_4']/ul/li/i[@class='fa fa-phone']


[@id='primary-menu']

deck

By Gulshan Nadaph