[Android - Java] Appium find child element incorrectly

My Appium version is 1.6.3 in Mac, and my Android device’s version is Android 7.1.1.

I am facing a problem that get child element from parent element is incorrect.

please check the screenshot below,
in my test case, I would like to verify the row “Call only” without “IM” button, and have “Call” button.

so my code similar as below:

// find all rows
List<MobileElement> rows = driver.findElementsByClassName("android.widget.RelativeLayout");

// get the target row "Call only"
MobileElement call_only_row;
for (MobileElement row : rows) {
    if (row.findElementById("....:id/lc_title").getText().equals("Call only")) {
        call_only_row = row;
        break;
    }
}

now I try to list all IM buttons from the parent element call_only_row;

List<MobileElement> ims = row.findElementById("...:id/mw_linearLayout2").findElemendByIds("...:id/lc_btn_chat");

and then print out the size of the IM buttons of element “call_only_row”, it should be ZERO, but actually the value is ONE.

System.out.println("Call only's IM button size: " + ims.size());    // output 1;

it is incorrect.

finally, I traced what “IM” button was found in ims list, I try to click this “IM” button which stored in ims list, and finally found up this “IM” button, is the next row’s “IM” button, the “IM” button of row IM only.

please check screenshot below.



Could you advise how I can get the correct child element from parent?

  1. better find rows by some ID if it exists - to eliminate cases of finding some other elements of RelativeLayout.

  2. try but not sure if help

row.findElement(MobileBy.id("....:id/lc_title")).getText().equals("Call only")

does not help - use:
3) now when you have row better search by “classname”

row.findElements(MobileBy.className("android.widget.TextView")).get(0).getText().equals("Call only")

Hi Aleksei, thanks for your answer.

  1. find rows now it is no problem as I checked only the table view’s row’s class name is “relativeLayout”, it is no problem to identify the rows.

  2. also found the row “Call only” is no problem, the code find this row is the correct row, because one textview’s id is “…:id/lc_title”, and another textview id is “…:id/lc_description”.

and then I tried those two solutions you provided, both are are the same result as before I got.

ok. so did you try:

row.findElement(MobileBy.className("android.widget.LinearLayout")).findElements(MobileBy.className("android.widget.ImageView"))

//another way as PageObject
@HowToUseLocators(androidAutomation = LocatorGroupStrategy.CHAIN)
@AndroidFindBy(id = "lc_title")
@AndroidFindBy(classname = "android.widget.LinearLayout")
@AndroidFindBy(classname = "android.widget.ImageView")
private List<AndroidElement> myIcon;

never try @HowToUseLocators(androidAutomation = LocatorGroupStrategy.CHAIN) before, will try it later.