Using direct XPath in your test
As I mentioned in the first part of this section, having //
as the start of your XPath is seen as a greedy query since it will parse the entire DOM until it finds the element that you want to find. If you want to work against an element that is always in a certain place, you can use a more direct XPath.
Finding elements by direct XPath
Instead of using //
, you can use a single /
, but you will need to make sure that the first node in your query is HTML. Let's see an example of this:
- Open Selenium IDE.
- Navigate to http://book.theautomatedtester.co.uk/chapter2.
- Type
xpath=/html/body/div[2]/div[3]/input
into the Target input of Selenium IDE. - Click on the Find button.
The previous locator will find the same element as before. This type of XPath query will find the element fractionally quicker, but if your UI is going to change, it may fail if the element is moved into a different area of the page. One thing to really note is that XPath locators can be extremely...