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.

[Android] element.findElements() returns wrong result

See original GitHub issue

Hi,

I’m using Appium@1.3.4 and Android 4.4.4.

Our app has a page that contains a list of TV channels. Each channel MAY have a little LIVE icon. This is how it looks on the UI : screenshot_5569483670517986414

And this is the UI hierarchy for the channel w/o LIVE icon :

2015-02-05_18h42_10 and with LIVE icon :

2015-02-05_18h42_34

At first I’m finding the channel element using :

WebElement firstChannel = driver.findElements(By.id("com.company.app:id/visibleChannel")).get(0);

Then I’m looking for child LIVE element under the channel. And the problem is that if the channel element does not contain LIVE icon, appium anyway returns some element. I found out that it is the nearest available LIVE icon from the nearest channel in the UI tree. So for that case the following code returns 1 :

firstChannel.findElements(By.id("com.company.app:id/listing_live_btn")).size();

And here’s corresponding appium log :

info: --> POST /wd/hub/session/e52db23d-bd9e-470f-98d7-1358a4f94afc/elements {"using":"id","value":"com.company.app:id/visibleChannel"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.company.app:id/visibleChannel","context":"","multiple":true}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.company.app:id/visibleChannel","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 com.company.app:id/visibleChannel using ID with the contextId:  multiple: true
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] getElements selector:UiSelector[RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (0)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (1)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=1, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (2)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=2, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (3)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=3, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (4)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=4, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (5)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=5, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (6)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=6, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (7)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=7, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Element[] is null: (8)
info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=8, RESOURCE_ID=com.company.app:id/visibleChannel]
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":[{"ELEMENT":"195"},{"ELEMENT":"196"},{"ELEMENT":"197"},{"ELEMENT":"198"},{"ELEMENT":"199"},{"ELEMENT":"200"},{"ELEMENT":"201"},{"ELEMENT":"202"}],"status":0}
info: [debug] Responding to client with success: {"status":0,"value":[{"ELEMENT":"195"},{"ELEMENT":"196"},{"ELEMENT":"197"},{"ELEMENT":"198"},{"ELEMENT":"199"},{"ELEMENT":"200"},{"ELEMENT":"201"},{"ELEMENT":"202"}],"sessionId":"e52db23d-bd9e-470f-98d7-1358a4f94afc"}
info: <-- POST /wd/hub/session/e52db23d-bd9e-470f-98d7-1358a4f94afc/elements 200 139.860 ms - 217 {"status":0,"value":[{"ELEMENT":"195"},{"ELEMENT":"196"},{"ELEMENT":"197"},{"ELEMENT":"198"},{"ELEMENT":"199"},{"ELEMENT":"200"},{"ELEMENT":"201"},{"ELEMENT":"202"}],"sessionId":"e52db23d-bd9e-470f-98d7-1358a4f94afc"}
info: --> POST /wd/hub/session/e52db23d-bd9e-470f-98d7-1358a4f94afc/element/195/elements {"id":"195","using":"id","value":"com.company.app:id/listing_live_btn"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.company.app:id/listing_live_btn","context":"195","multiple":true}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.company.app:id/listing_live_btn","context":"195","multiple":true}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding com.company.app:id/listing_live_btn using ID with the contextId: 195 multiple: true
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[RESOURCE_ID=com.company.app:id/listing_live_btn]
info: [debug] [BOOTSTRAP] [debug] getElements selector:UiSelector[RESOURCE_ID=com.company.app:id/listing_live_btn]
info: [debug] [BOOTSTRAP] [debug] Element[195] is 195, counter: 0
info: [debug] [BOOTSTRAP] [debug] Element[195] is 195, counter: 1
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":[{"ELEMENT":"203"}],"status":0}
info: [debug] Responding to client with success: {"status":0,"value":[{"ELEMENT":"203"}],"sessionId":"e52db23d-bd9e-470f-98d7-1358a4f94afc"}
info: <-- POST /wd/hub/session/e52db23d-bd9e-470f-98d7-1358a4f94afc/element/195/elements 200 37.645 ms - 91 {"status":0,"value":[{"ELEMENT":"203"}],"sessionId":"e52db23d-bd9e-470f-98d7-1358a4f94afc"}

Interesting thing is that if I will use Xpath then corresponding code will return 0 as expected :

driver.findElementsByXPath("//*[@resource-id='com.company.app:id/visibleChannel'][1]//*[@resource-id='com.company.app:id/listing_live_btn']").size()

Also log for that case :

info: --> POST /wd/hub/session/e52db23d-bd9e-470f-98d7-1358a4f94afc/elements {"using":"xpath","value":"//*[@resource-id='com.company.app:id/visibleChannel'][1]//*[@resource-id='com.company.app:id/listing_live_btn']"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//*[@resource-id='com.company.app:id/visibleChannel'][1]//*[@resource-id='com.company.app:id/listing_live_btn']","context":"","multiple":true}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//*[@resource-id='com.company.app:id/visibleChannel'][1]//*[@resource-id='com.company.app:id/listing_live_btn']","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 //*[@resource-id='com.company.app:id/visibleChannel'][1]//*[@resource-id='com.company.app:id/listing_live_btn'] using XPATH with the contextId:  multiple: true
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":[],"status":0}
info: [debug] Condition unmet after 85ms. Timing out.
info: [debug] Responding to client with success: {"status":0,"value":[],"sessionId":"e52db23d-bd9e-470f-98d7-1358a4f94afc"}
info: <-- POST /wd/hub/session/e52db23d-bd9e-470f-98d7-1358a4f94afc/elements 200 90.834 ms - 74 {"status":0,"value":[],"sessionId":"e52db23d-bd9e-470f-98d7-1358a4f94afc"}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
prattprattcommented, Feb 24, 2015

Ok, here’s how it could be done:

  • on the Google Play Market find and install Horizon Go app: https://play.google.com/store/apps/details?id=com.lgi.orionandroid
  • select any country (e.g. Ireland), skip Sign in page and go to TV Guide page
  • tap Search icon on the top-right corner and type any short search keyword (like th)
  • you will see a list of TV programs (search items) and some of them will have LIVE icon
  • try to find an item without LIVE icon, and then try to search for LIVE icon inside it:
WebElement e = driver.findElements(By.id("com.lgi.orionandroid:id/stickyItem")).get(0);
e.findElements(By.id("com.lgi.orionandroid:id/listing_live_btn")).size();
/* (int) 1 */
  • if you will execute findElements() and use XPath you will see other result - as expected:
driver.findElements(By.xpath("//*[@resource-id='com.lgi.orionandroid:id/stickyItem'][1]//*[@resource-id='com.company.app:id/listing_live_btn']")).size();
/* (int) 0 */
  • screen layout for that case may look smth like this : image
0reactions
lock[bot]commented, Apr 27, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Appium Finding Wrong Element when Finding With xPath
Try using the Appium inspector to search for your button. You can type in the xpath and search for the element to see...
Read more >
Appium is sometimes clicking on wrong element - Support
I am trying to click on an element on my Android Hybrid app. Sometimes it clicks on correct element and sometimes it's clicking...
Read more >
Why does element.displayed? throws exception instead of ...
I expected the code to return false when the element was not found but instead I got the exception. Is this working as...
Read more >
FindElement And FindElements In Selenium [Differences]
findElements in Selenium returns you the list of web elements that match the locator value, unlike findElement, which returns only a single web ......
Read more >
findElement vs findElements in Selenium - BrowserStack
The findElements command returns an empty list if no elements are found using the given locator strategy and locator value.
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