Retrieving children of an element

Hello everyone,

I am unsure how to retrieve the direct children of an element. When I ask Appium for a list of elements by calling findElements on a WebElement object, (great-)* grandchildren of the WebElement is returned in addition to the direct children. I was thinking about using Xpath by sending “./*”, but I’ve learned and read from a lot of sources that Xpath is wonky and should be avoided.

How can I retrieve just the direct children of an element without using Xpath?

2 Likes

Using ccs try selector:

"element > *"

Hi @kirill,

I don’t believe Appium implements the CSS selector strategy. My logs from Appium are:

info: --> POST /wd/hub/session/e1b2345f-228c-465b-a5ce-5c95c4cfdbb5/elements {"using":"css selector","value":"UIATableCell > UIAStaticText"}
info: [debug] Invalid locator strategy: css selector
info: [debug] Responding to client with error: {"status":9,"value":{"message":"Invalid locator strategy: css selector","origValue":"Invalid locator strategy: css selector"},"sessionId":"e1b2345f-228c-465b-a5ce-5c95c4cfdbb5"}
info: <-- POST /wd/hub/session/e1b2345f-228c-465b-a5ce-5c95c4cfdbb5/elements 500 0.482 ms - 177

I did, however, come across this SO post. http://stackoverflow.com/questions/25806765/how-to-use-css-selector-instead-of-xpath-in-appium-1-2-2-version-for-ios-applica

Appium implements the CSS selector strategy but within web context, obviously. From the logs I can see, that you are trying to use css selector within native context, which simply won’t work. Just use css selector within web context.

Hi again @kirill,

I did a bit more reading about contexts, and it seems that the contexts are used mostly in hybrid applications where some web view is embedded into the app. The portion of the app I am testing at the moment does not make much use of web views, and the child elements I want to locate are native UI elements, not web elements. To be sure that my understanding of CSS selectors was accurate however, I gave CSS selectors a second try in a web view context, and it is neat to see the call return a valid, although empty, result set.

Thank you for your input, regardless. I learned something new from it. :smiley: I will continue by looking at the uiautomation strategy.

@awang, I thought you are working with web views, since you mentioned about WebElement objects. CSS is pure web stuff. Xpath is the only way to get what you need within native context.

Sorry. That was my fault for mixing up terminology. Since the elements returned are WebElement objects, I ended up referring to them as WebElements in general. I will be sure to explicitly call elements MobileElements or WebElements now.

That’s ok. I just want to make sure I got you correctly and you are able to solve the problem. Hope that helped :smile:

I just learned about XPATH axis, it provides a LOT of useful methods for locating specific elements via relationships to other elements in the DOM, and you can continue to find very specific elements via attributes such as name.

Start here as it has clear, well detailed examples: http://www.xmlplease.com/axis