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:

  1. Open Selenium IDE.
  2. Navigate to http://book.theautomatedtester.co.uk/chapter2.
  3. Type xpath=/html/body/div[2]/div[3]/input into the Target input of Selenium IDE.
  4. Click on the Find button.

The previous locator will find the same element as before. ...

Get Learning Selenium Testing Tools - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.