Advanced dynamic XPath in Selenium to reduce failure rate

Category: Selenium WebDriver 64 0

We have already discussed the technique to create dynamic XPath in Selenium WebDriver. Just for the revision, we discussed earlier- basics & definition of XPath, various XPath methods like contains, following, preceding and different uses of operators within the dynamic XPath. As we all know XPath is a language to lookup attributes in an XML file. Today we are going to discuss some advanced dynamic XPath technique in Selenium. These methods will help you to traverse in DOM very efficiently.

If you find web elements perfectly or if you create perfect locators through XPath then you are done with 40% part of your Selenium test case. Further, remaining things are easier as you only have to call the required methods which are written within the Selenium library with little programming logic in Java, Python, Ruby or any other programming language which you wish to use for the Automation testing.

Before we start further, I’d recommend you to go through following tutorials which helps you in solving most of your problems relating to XPath, CSS Selector, and different locators. It is highly recommended that you go through below tutorials first then you come again to this tutorial on advanced dynamic XPath Selenium.

Hope you have finished reading tutorials. Let’s start with the advanced dynamic XPath guide.

List of Advanced dynamic XPath methods to identify elements efficiently

Here is list of some of the advanced dynamic XPath methods that we are going to discuss today. Let’s have a look:

  • ancestor
  • ancestor-or-self
  • descendant
  • descendant-or-self
  • parent
  • child
  • preceding-sibling
  • following-sibling

These methods look-up in DOM based on their defined axis. Let’s begin with the ancestor in details, further, we will discuss rest of the methods too.

Don’t Miss: Actions class in Selenium for Mouse and Keyboard events

Dynamic XPath using- ‘ancestor’

It sends us up to the parent node and above. Simply, it looks up web elements up to the root node.Advanced Dynamic XPath

Advanced Dynamic XPath Ancestor

Dynamic XPath using- ‘ancestor-or-self’

It sends us to the current node and its ancestor only.

Advanced Dynamic XPath ancestor or self

It displays same number of elements as the ancestor.

Dynamic XPath using- ‘descendant’

It traverses opposite to the parent node. It sends us to the children and up to their children’s node until it reaches to the leaf.

Advanced Dynamic XPath Desendant inviul

Advanced Dynamic XPath Desendant

Dynamic XPath using- ‘descendant-or-self’

It traverses the current node and its children only.

Advanced Dynamic XPath Desendant

Dynamic XPath using- ‘parent’

It traverses to the parent node of the context node.

Advanced Dynamic XPath Parent Gmail

Advanced Dynamic XPath Parent

Dynamic XPath using- ‘child’

It sends us to the child of the context node.

Advanced Dynamic XPath Child inviul

Advanced Dynamic XPath Child

Dynamic XPath using- ‘preceding-sibling’

It sends you to the left of the parent node. Simply, it traverses to the elder siblings of the context element.

Advanced Dynamic XPath Preceding sibling Inviul

Advanced Dynamic XPath Preceding sibling

Dynamic XPath using- ‘following-sibling’

It sends you to the right of the parent node. Simply, it traverses to the younger siblings of the context element.

Advanced Dynamic XPath Following sibling gmail

Advanced Dynamic XPath Following sibling

Note: preceding-sibling & following-sibling shares the common parents.

By looking to the above XPath methods, we see how they help to establish a relative connection among rest of the elements. It looks like a family and their strong bonding. This is why they named relation XPath.

Apart from above methods, there are few more methods like namespace, self, and attribute. But, you don’t need to use these two since most of your work will be done by above discussed advanced dynamic XPath methods and some methods which have been discussed in the previous post on dynamic XPath.

 

 

Related Articles

Add Comment