Book Image

Learning Selenium Testing Tools - Third Edition

Book Image

Learning Selenium Testing Tools - Third Edition

Overview of this book

Table of Contents (22 chapters)
Learning Selenium Testing Tools Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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. 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 fragile...