Use xpath or css selector to parse whole element in hybrid mobile app

I am currently working to test mobile UI automation. Our app is a hybrid mobile app based on Cordova. So I am planning on using appium to run some automation tests.

I need to figure out how to find all the elements on a page.

Previously, I was planning to use xpath to find all elements as we can detect the xpath via the appium inspector. However, my colleague disagrees with me as he wants to use css selectors as a key to find all elements in the mobile app. But for appium, it doesn't show the css selector in the inspector.

So, I'm just curious which approach should be better?



source to share

1 answer

Below is a breakdown of locators and how / why I love using them for iOS Automation. This is all experimentally based on my work with Native iOS apps.

Giant disclaimer: I don't know anything about Cordoba. I heard that there are problems that exist with UIAutomation if there are class names that are not native. If so, I suggest sticking to the accessibility identifier and class name defining strategy.

Locators for iOS Automation

CSS selectors

CSS selectors don't exist in Appium.

Class name

The closest you get to a CSS selector is the class name selector. I don't really use them because UIAutomation gives me what I need and allows me to validate the element name / text in the locator strategy.


You don't want to use XPath because it is slow and thin on iOS. (It can sometimes return a completely wrong item.) Sometimes this can lead to malfunctions of instruments for no reason. I highly recommend staying away from XPath.

Accessibility ID

You should use this when UIAutomation cannot find the element. It's faster than XPath, but useful when UIAutomation doesn't let you in an element ( .actionSheets()

broken I guess. I use this when the action sheet is up and I need .click()



You must use Apple UIAutomation , 2 , as it is the fastest native iOS solution.

The UIAutomation framework allows you to use classes and hierarchy to specify which element you want. When using Appium, use the function find_elements_by_ios_uiautomation

on your web server.

Usage example here

But the use case doesn't tell you how powerful UIAutomation really is. A common problem I am facing is trying to find all the cells of a table when there were multiple table views on the screen.

Find all UIACells for UIATableview Shopping Cart

**Sample View Hierarchy** 

        <table name="Items">
          <cell name="Foo, not in cart"></cell>
        <table name="Cart">
          <cell name="Bar. IM IN YOUR CART"></cell>


OK to find these cells in the array:

value = '.tableviews()["Cart"].cells()'
cells = driver.find_elements_by_ios_uiautomation(value)


Additional reading: A guide that lists predicates and why they are awesome

Limitations for UIAutomation

  • If your element has no visible name. (Developers love to paint invisible buttons behind "Hint Overlays" etc.).
    • Recommended solution: add an accessibility identifier, use an accessibility identifier locator.

Testing locator strategies

There is a nice place in the Inspector (presented in the GUI) that allows you to try any strategy and value of the locator. You have to use it. It helps so much.

Testing Locators with Inspector



All Articles