Select Page
Selenium Testing

How to use XPath Axes in Selenium Webdriver

In this blog article, you will learn how to use XPath Axes in Selenium WebDriver.

How to use XPath Axes in Selenium Webdriver
Listen to this blog

Using the CSS selector, you cannot traverse to the parent node/grandparent nodes on the DOM tree. The advantage with the XPath locator is you can traverse between child & descendant and parent & ancestors. To use XPath effectively in Selenium WebDriver, you should be familiar with XPath Axes and HTML. In this blog article, you will learn how to use XPath Axes in Selenium WebDriver.

What are XPath Axes?

There are thirteen axes available for XPath. As per the definition in Mozilla Web Docs – “An axis represents a relationship to the context node, and is used to locate nodes relative to that node on the tree.”

Let’s say you want to select a parent node using its child node. You first write XPath to pick the child node and the parent node using XPath parent axis.

As an automation tester, if you are familiar with XPath Axes, you can write robust locators for dynamic WebElements. At Codoid, we train our novice test automation engineers on Selenium Basics and Object Locating Strategies before they are inducted for automation testing services. In the training phase, XPath Axes is one of the core topics.

Let’s look into all the XPath Axes which are useful for test automation scripting.

parent

If you want to select the parent node from the child, use the parent axis.
As the below diagram, first, it locates the IMG tag and then selects the parent node (i.e., SPAN tag)

XPath Axes in Selenium Webdriver

ancestor-or-self

ancestor-or-self axis selects the parent, ancestors, and the context node as well.

Let’s say there is a DIV tag; you want to identify its parent & ancestors DIVs, and additionally the context node if that is also a DIV tag. If this is the situation you face during Selenium Automation Testing, you can use ancestor-or-self axis.

XPath Axes

child

To select all the children nodes of the context node, you can use the child axis.

locators in selenium

descendant

To select children and the descents nodes, use the descendant axis. The descendant axis is a widely used axis by automation testers. However, the below diagram will be a good learning material for novice testers who are learning Selenium.

descendant

following-sibling

If you would like to select siblings that are after the current node, try the following-sibling axis.

xpath following-sibling

preceding-sibling

If you would like to select siblings that are before the current node, try the preceding-sibling axis.

xpath preceding-sibling

Conclusion

XPath Axes are helpful to traverse the DOM tree to locate dynamic web objects. Selenium Tester should know each XPath axis and its use to create robust automation test scripts.

For mobile apps automation testing, locating techniques are different. In the subsequent blog articles, we will be publishing more on mobile app test automation using Appium. As a leading automation testing company, we have started our Singapore operations and planned to schedule automation testing training sessions for professionals in the Singapore region.

If you are in Singapore and want to learn Object Locating Concepts, Advanced Selenium, & Appium, please contact us.

Comments(2)

  • 1 year ago

    Pretty! This has been a really wonderful article. Many thanks for supplying this information.

  • 1 year ago

    An outstanding share! I've just forwarded this onto a co-worker who had been conducting a little research on this. And he in fact ordered me lunch simply because I discovered it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanx for spending some time to talk about this topic here on your site.

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility