Get element from row in android.widget.listview

In the android.widget.ListView there are rows with various elements… Text and button with overflow resource-id.
so the hierarchy is

I want to click the ImageView corresponding to the TextView=DOC Test.

I can click the ImageView with class name and index value but it is not corresponding to the text. It is simple hardcoding the index value.

Any way I can achieve this?

You can use the xpath. But a better solution could be to get the list of textview elements and then iterate the list to pick up the right element you want. Something like this:

List <WebElement> textViews = driver.findElementsByClassName(“android.widget.TextView”);

and then iterate through the list of elements and get and compare the “text” property for each element to identify the required element

You can also achieve this using UiSelectors, namely “fromParent”.

new UiSelector().className(“android.widget.TextView”).resourceId(“DOC Test”).fromParent(new UiSelector().className(“android.widget.ImageView”).instance(1));

Something like that, sorry I couldn’t test it.

args= “new UiSelector().className(“android.widget.ListView”).childSelector(new UiSelector().className(“android.widget.RelativeLayout”).childSelector(new UiSelector().childSelector(new UiSelector().className(“android.widget.TextView”).textContains(“DOC Test”))).fromParent(new UiSelector().resourceId(“”)));”

Here is what i am trying but it returns an element cannot be found.
This is how it is parsed in the appium logs:
UiSelector[CLASS=android.widget.ListView, CHILD=UiSelector[CLASS=android.widget.RelativeLayout, CHILD=UiSelector[CHILD=UiSelector[CONTAINS_TEXT=DOC Test, CLASS=android.widget.TextView, PARENT=UiSelector[]]]]]

Did you get a chance to check the solution I proposed. That might work for you

Sorry Umar, i missed your post. I will try that and let you know.