- Learning Selenium Testing Tools(Third Edition)
- Raghavendra Prasad MG
- 567字
- 2025-02-22 16:26:18
Leveraging the XPath axis with elements
As we have seen, XPath is normally only used if the element we need to interact with is not accessible by normal means. In this section of the chapter, we will have a look at leveraging the XPath axis in our queries to find the element that we wish to interact with. An example that I have used in the real world was to find a table cell that had specific text, then traverse the tree backwards to find the edit button so that I can click on it. This may seem laborious just to click on an edit button, but it is extremely common according to the Selenium users forum on Google Groups.
Using the XPath axis
In the first example, we found a button and then its sibling. In this example, the query that we will generate is equivalent to xpath=//div[@class='leftdiv']/input[2]
.
- We will start by finding the first element for our query, which is
//input[@value='Button with ID']
. Place this element into the Selenium IDE Target textbox and see which element it highlights. - 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 the element,
following-sibling
, that we need. 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 will be able to find the element that we are after; see the following screenshot:
We just saw how we can use the XPath axis (an axis defines a node-set relative to the current node) 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 preceding example and reverse it, you would 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. Finding the element using the Xpath axis is shown in the following screenshot:

The following is a list of axes that you can use in your XPath queries to find the elements on the page:

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 this can slow down your test. Browsers such as 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 explained in the next section of this book.
Note
Working with the XPath axis
Go to http://financial-dictionary.thefreedictionary.com/ and use contains()
, starts-with()
and ends-with()
on the page. Use the getXPathCount()
call to see how many items you can get with your XPath query.