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