Time For Action – using XPath Axis - Selenium

In the first example, we are going to find a button and then find its sibling. In this example, the query that we will generate is equivalent to xpath=//div[@class='leftdiv']/ input[2] .

  1. We will start by finding the first element for our query which is //input[@ value='Button with ID']. Place that into Selenium IDE Target textbox and see which element it highlights.

  2. There is another button below the one that is highlighted and that is the element that we need to work with in this section. The button is the next input item in the HTML, so it is elements following-sibling that we need.

  3. Our locator will look like //input[@value='Button with ID']/following-sibling::input[@ value='Sibling Button'] and if it was placed into Selenium IDE it would be able to find the element that we are after; see the following screenshot:

What just happened?

We have just seen how we can use XPath axis to find the elements that we need in our tests. We managed to find the element using the following-sibling axis.

As mentioned earlier you can use XPath to find an element and then walk backwards up the tree. If we were to take the example that we have just done and reverse it, you will need to start at the button with the value Sibling Button and then go back to the button with the value Button with ID the XPath query would then look like.

We can see it finding the element in the following screenshot:

Following is a list of Axis that you can use in your XPath queries to find the elements on the page:

Axis name



Selects all the ancestors (parent, grandparent, and so on) of the element


Selects all the descendants (children, grandchildren, and so on) of the element


Selects all elements that follow the closing tab of the current element


Selects all the siblings after the current element


Selects the parent of the current element


Selects all elements that are before the current element


Selects all of the siblings before the current element

As we have seen, there is a large number of different ways to find the same element on the web page. Having XPath queries in your test can be really useful for finding elements on the page but can slow down your test. Browsers like Internet Explorer 6 do not have built-in XPath libraries and rely on doing the XPath query via JavaScript which can mean that a test that uses XPath can run two or more times slower than a test with IDs. The more complex the XPath, the slower the test since it needs to do more DOM traversals which is an expensive operation.

There is also another way to do XPath-like queries against the DOM and use built-in libraries in most browsers. We can use CSS selectors which is the next section of this book.

Pop quiz - using XPath Axis

  1. Pick two from the following if you wanted do a partial match on an attribute on an element from the beginning of the value:

    1. contains()

    2. starts-with()

    3. ends-with()

Have a go hero - working with XPath Axis

Go to http://financial-dictionary.thefreedictionary.com/ and use contains(), starts-with(), and ends-with() on the page. Use the call getXPathCount() to see how many items you can get with your XPath query.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics