Need help Using Xpath more efficiently

so i read xpath is bad in the long run and others saying its because most people dont know how to use it correctly. I was wondering if someone can explain to me using the more advanced techniques of Xpath. Something along the lines of

‘//*[@name= nameMe]’

Since appium 1.5 took out Find i was hoping to replace it with xpaths that dont break as easily…

i have a screen shot below if someone could maybe write me an xpath for it using the name or value or label and maybe explain it a little bit would be so great and helpful. Thanks!

I only know a few things about how to make decent Xpaths. Making excellent Xpath expressions is a bit of an art.

  1. Do not use absolute Xpaths. They’re incredibly brittle, and Appium inspector only shows it to you as a guideline.
  2. Attributes and Xpath axes as your friends.

This can help you: xpath_syntax and xpath_axis

Thanks guys ill take a look at them.

Your simplistic xpath you scoped is perfectly fine as long as it is ONLY finding the target element(s) you are profiling. The risk of using ‘//*[@name= nameMe]’ is that it will simply return the first element with that name attribute. If your page will only ever have just one element with that name, then it works.

However, if it has more than one element with that name then you’re in trouble as it will simply return the first matching element in the DOM. So I usually add the element type, in your case ‘//UIAButton[@name= nameMe]’ Now it will return the first Button with that name attribute, but not a UIATableView, UIATableGroup, UIATextField, etc.

Another reason I hear about not using XPath is that it’s slow/slower than by ID/name, and CSS, at least in the web world. I wonder if there is any translation of that to native apps side, or whether that’s actually irrelevant here in terms of native apps (e.g. find by ID, name, vs XPath, etc.).

XPath is considered one of the slower methods for finding elements. It’s not too bad on Android tests, but it can be devastatingly slow on iOS (I have not had a chance to touch the new iOS automation framework and library, yet though. The story might be different there!).

What is the new framework you are talking about and where can i get documentation on it, i would love to know.


The new iOS automation framework is known as XCUITest. I first heard about it from this post: [iOS9 UIAutomation] What is Appium approach to UIAutomation deprecation by Apple