question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findElementsByAndroidUIAutomator can only get the first element ,not all elements if uiAutomator expression with childSelector or fromParent ?

See original GitHub issue
java-client : 2.1.0
appium server : 1.3.4

uiautomatorviewer : 12345

i want to get all the android.widget.RelativeLayout elements which is child of the ListView.

java code:

AndroidDriver appDriver;
List<WebElement> list = appDriver.findElementsByAndroidUIAutomator("new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\"))");
System.out.print(list.size());
for(int i = 0; i <= list.size() - 1; i++){
     System.out.println("i==" + i + " " + list.get(i).getLocation().getX());
     System.out.println("i==" + i + " " + list.get(i).getLocation().getY());
}

it only find the first android.widget.RelativeLayout which is child of android.widget.ListView.

appium.log:

 > info: --> POST /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16/elements {"using":"-android uiautomator","value":"new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\"))"}
> info: [debug] Waiting up to 1000ms for condition
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"-android uiautomator","selector":"new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\"))","context":"","multiple":true}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"-android uiautomator","selector":"new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\"))","context":"","multiple":true}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().className("android.widget.RelativeLayout")) using ANDROID_UIAUTOMATOR with the contextId:  multiple: true
> info: [debug] [BOOTSTRAP] [debug] Parsing selector: new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().className("android.widget.RelativeLayout"))
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: java.lang.Class<T> arg: "android.widget.ListView"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: "android.widget.ListView"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class com.android.uiautomator.core.UiSelector arg: new UiSelector().className("android.widget.RelativeLayout")
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: java.lang.Class<T> arg: "android.widget.RelativeLayout"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: "android.widget.RelativeLayout"
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[CLASS=android.widget.ListView, CHILD=UiSelector[CLASS=android.widget.RelativeLayout]]
> info: [debug] [BOOTSTRAP] [debug] getElements selector:UiSelector[CLASS=android.widget.ListView, CHILD=UiSelector[CLASS=android.widget.RelativeLayout]]
> info: [debug] [BOOTSTRAP] [debug] Element[] is null: (0)
> info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[CLASS=android.widget.ListView, INSTANCE=0, CHILD=UiSelector[CLASS=android.widget.RelativeLayout]]
> info: [debug] [BOOTSTRAP] [debug] Element[] is null: (1)
> info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[CLASS=android.widget.ListView, INSTANCE=1, CHILD=UiSelector[CLASS=android.widget.RelativeLayout]]
> info: [debug] [BOOTSTRAP] [debug] Updating class "class com.android.uiautomator.core.UiDevice" to enable field "mUiAutomationBridge"
> info: [debug] [BOOTSTRAP] [debug] Updating class "class com.android.uiautomator.core.UiAutomatorBridge" to enable field "mInteractionController"
> info: [debug] [BOOTSTRAP] [debug] Finding methods on class: class com.android.uiautomator.core.UiObject
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":[{"ELEMENT":"22"}],"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":[{"ELEMENT":"22"}],"sessionId":"5bd47fe5-edc4-461c-a40a-47eda889cd16"}
> info: <-- POST /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16/elements 200 111.159 ms - 90 {"status":0,"value":[{"ELEMENT":"22"}],"sessionId":"5bd47fe5-edc4-461c-a40a-47eda889cd16"}
> info: --> GET /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16/element/22/location {}
> warn: location will be removed in a future version of Appium, please use location_in_view
> info: [debug] Pushing command to appium work queue: ["element:getLocation",{"elementId":"22"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getLocation","params":{"elementId":"22"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getLocation
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"y":272,"x":0},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"y":272,"x":0},"sessionId":"5bd47fe5-edc4-461c-a40a-47eda889cd16"}
> info: <-- GET /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16/element/22/location 200 41.363 ms - 87 {"status":0,"value":{"y":272,"x":0},"sessionId":"5bd47fe5-edc4-461c-a40a-47eda889cd16"}
> info: --> GET /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16/element/22/location {}
> info: [debug] Pushing command to appium work queue: ["element:getLocation",{"elementId":"22"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getLocation","params":{"elementId":"22"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getLocation
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"y":272,"x":0},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"y":272,"x":0},"sessionId":"5bd47fe5-edc4-461c-a40a-47eda889cd16"}
> info: <-- GET /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16/element/22/location 200 25.430 ms - 87 {"status":0,"value":{"y":272,"x":0},"sessionId":"5bd47fe5-edc4-461c-a40a-47eda889cd16"}
> info: --> DELETE /wd/hub/session/5bd47fe5-edc4-461c-a40a-47eda889cd16 {}

and use fromParent can only get the first element: code:

 List<WebElement> list2 = appDriver.findElementsByAndroidUIAutomator("new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\")).fromParent(new UiSelector().className(\"android.widget.RelativeLayout\"))");
        System.out.print(list2.size());
        for(int i = 0; i <= list2.size() - 1; i++){
            System.out.println("i==" + i + " " + apm.getCenterY(appDriver,list2.get(i)));
            System.out.println("i==" + i + " " + list2.get(i).getLocation().getY());
        }

appium.log

> info: --> POST /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/elements {"using":"-android uiautomator","value":"new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\")).fromParent(new UiSelector().className(\"android.widget.RelativeLayout\"))"}
> info: [debug] Waiting up to 1000ms for condition
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"-android uiautomator","selector":"new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\")).fromParent(new UiSelector().className(\"android.widget.RelativeLayout\"))","context":"","multiple":true}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"-android uiautomator","selector":"new UiSelector().className(\"android.widget.ListView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\")).fromParent(new UiSelector().className(\"android.widget.RelativeLayout\"))","context":"","multiple":true}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().className("android.widget.RelativeLayout")).fromParent(new UiSelector().className("android.widget.RelativeLayout")) using ANDROID_UIAUTOMATOR with the contextId:  multiple: true
> info: [debug] [BOOTSTRAP] [debug] Parsing selector: new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().className("android.widget.RelativeLayout")).fromParent(new UiSelector().className("android.widget.RelativeLayout"))
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: java.lang.Class<T> arg: "android.widget.ListView"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: "android.widget.ListView"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class com.android.uiautomator.core.UiSelector arg: new UiSelector().className("android.widget.RelativeLayout")
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: java.lang.Class<T> arg: "android.widget.RelativeLayout"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: "android.widget.RelativeLayout"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class com.android.uiautomator.core.UiSelector arg: new UiSelector().className("android.widget.RelativeLayout")
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: java.lang.Class<T> arg: "android.widget.RelativeLayout"
> info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: "android.widget.RelativeLayout"
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[CLASS=android.widget.ListView, CHILD=UiSelector[CLASS=android.widget.RelativeLayout, PARENT=UiSelector[CLASS=android.widget.RelativeLayout]]]
> info: [debug] [BOOTSTRAP] [debug] getElements selector:UiSelector[CLASS=android.widget.ListView, CHILD=UiSelector[CLASS=android.widget.RelativeLayout, PARENT=UiSelector[CLASS=android.widget.RelativeLayout]]]
> info: [debug] [BOOTSTRAP] [debug] Element[] is null: (0)
> info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[CLASS=android.widget.ListView, INSTANCE=0, CHILD=UiSelector[CLASS=android.widget.RelativeLayout, PARENT=UiSelector[CLASS=android.widget.RelativeLayout]]]
> info: [debug] [BOOTSTRAP] [debug] Element[] is null: (1)
> info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[CLASS=android.widget.ListView, INSTANCE=1, CHILD=UiSelector[CLASS=android.widget.RelativeLayout, PARENT=UiSelector[CLASS=android.widget.RelativeLayout]]]
> info: [debug] [BOOTSTRAP] [debug] Updating class "class com.android.uiautomator.core.UiDevice" to enable field "mUiAutomationBridge"
> info: [debug] [BOOTSTRAP] [debug] Updating class "class com.android.uiautomator.core.UiAutomatorBridge" to enable field "mInteractionController"
> info: [debug] [BOOTSTRAP] [debug] Finding methods on class: class com.android.uiautomator.core.UiObject
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":[{"ELEMENT":"22"}],"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":[{"ELEMENT":"22"}],"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: <-- POST /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/elements 200 242.106 ms - 90 {"status":0,"value":[{"ELEMENT":"22"}],"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: --> GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/displayed {}
> info: [debug] Pushing command to appium work queue: ["element:getAttribute",{"elementId":"22","attribute":"displayed"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getAttribute","params":{"elementId":"22","attribute":"displayed"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getAttribute
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"true","status":0}
> info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: <-- GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/displayed 200 53.573 ms - 76 {"status":0,"value":true,"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: --> GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/location {}
> warn: location will be removed in a future version of Appium, please use location_in_view
> info: [debug] Pushing command to appium work queue: ["element:getLocation",{"elementId":"22"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getLocation","params":{"elementId":"22"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getLocation
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"y":272,"x":0},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"y":272,"x":0},"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: <-- GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/location 200 26.538 ms - 87 {"status":0,"value":{"y":272,"x":0},"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: --> GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/size {}
> info: [debug] Pushing command to appium work queue: ["element:getSize",{"elementId":"22"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getSize","params":{"elementId":"22"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getSize
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"height":276,"width":1440},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"height":276,"width":1440},"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: <-- GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/size 200 25.118 ms - 99 {"status":0,"value":{"height":276,"width":1440},"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: --> GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/location {}
> info: [debug] Pushing command to appium work queue: ["element:getLocation",{"elementId":"22"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getLocation","params":{"elementId":"22"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getLocation
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"y":272,"x":0},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"y":272,"x":0},"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}
> info: <-- GET /wd/hub/session/d8f97ee6-8efc-41b8-86ac-73b0159ace07/element/22/location 200 23.356 ms - 87 {"status":0,"value":{"y":272,"x":0},"sessionId":"d8f97ee6-8efc-41b8-86ac-73b0159ace07"}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jcyrsscommented, Jun 5, 2017

closed ,why? I met the same issue with python client . And I really doubt it is a bug of appium server.

with the following , I get only one element returned. But actually we have 4 elements matched.

code = 'new UiSelector().resourceId("io.manong.developerdaily:id/tab_bar").childSelector(new UiSelector().className("android.widget.TextView"))'
eles = driver.find_elements_by_android_uiautomator(code)

but only append a ‘instance(3)’ at the end of java code statment, I get another element matched.

code = 'new UiSelector().resourceId("io.manong.developerdaily:id/tab_bar").childSelector(new UiSelector().className("android.widget.TextView").instance(3))'
eles = driver.find_elements_by_android_uiautomator(code)

It seems obviously the issue of appium server.

0reactions
Jonahsscommented, Mar 1, 2019

Oh yeah. It’s definitely not an issue with the client. All the clients just send this as text to be interpreted by Appium.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Android UiSelector with Appium chained for child ...
I was actually looking at this question hoping for an answer. While testing more on my own side, I believe the following should...
Read more >
How to use UiSelector with Appium in java
Hi , I want to use the uiselector & uiscrollable capability with appium in java Do i need to import the uiautomator.jar or...
Read more >
UiSelector | Android Developers
Specifies the elements in the layout hierarchy for tests to target, filtered by properties such as text value, content-description, class name, ...
Read more >
FindsByAndroidUIAutomator (java-client 6.1.0 API)
Parameters: using - an Android UIAutomator selector; Returns: The first element that matches the given selector; Throws: org.openqa.selenium.
Read more >
Class androidx.test.uiautomator.UiSelector
Set the search criteria to match the visible text displayed in a layout element, using a regular expression. public UiSelector, textStartsWith(java.lang.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found