How to get list of child elements of an element in Appium?

Artem picture Artem · Dec 13, 2015 · Viewed 16k times · Source

I'm working with native iOS app through Appium.

I have the following structure:

UIAApplication ->
                  UIAWindow ->
                              UIATextBox
                              UIALabel
                  UIAWindow ->
                              SomethingElse

I have found a way to get the first UIAWindow and I'd like to get the list of all elements in that window. How do I do that?

I'd like to get UIATextBox and UIALabel from first UIAWindow but NOT SomethingElse element.

How do I do list of child elements in general?

  @Test
  public void testListWindows() {
      List<MobileElement> windows = driver.findElementsByClassName("UIAWindow");
      List<MobileElement> elements = windows.get(0).?????
  }

Answer

econoMichael picture econoMichael · Dec 15, 2015

You're almost there. What you have is giving you a list of all UIAWindows when what you want is a list of all the elements of the first UIAWindow. I'd suggest using Xpath with a wildcard.

This will get you every element that is a direct child of the first UIAWindow:

List<MobileElement> elements = driver.findElements(MobileBy.xpath("//UIAWindow[1]/*");

And this will get you every child and sub-child and sub-sub-child etc. of the first UIAWindow:

List<MobileElement> elements = driver.findElements(MobileBy.xpath("//UIAWindow[1]//*");

An extra tip: If you're automating for iOS, I assume that means you have access to OS X, where you can install the Appium dot app and use inspector. The inspector is a great tool to test out xpath queries before putting them into your code.