How to get a random element from a list except any other specific ones?

Hello,

I use Appium at the moment to create some automated tests for an Android app. The language used is Java.

My problem is this:
I have a list of objects out of which I had to get one of the elements with a specific ID. That was simple, I used d.findElement(By.id(“id”);

Now I need to get, from the same list, one of the other elements. Unfortunately these don’t have specific IDs, so I can’t use the same method. I also can’t get a specific one as the list will surely change and I need a reliable test.

So the only thing I can cling on would be either
‘one of the elements that don’t have that specific ID’
or
‘one of the elements that don’t have the index 4 in the list’

Is there a way to do that?

P.S. - This is how the tree looks like (I had to remove the texts for privacy reasons):

driver.findElements(By.className(“com.android.textview”)).get(5).getText();

Thanks for the reply!
The problem is that I need a random one.
I have a list of x elements, in random order, that have ExpandableListView as parent.
Some of them have a label. Those I got with find.by.id (as the label has an ID).
Some don’t have a label. I don’t know how to get those :smile:

Never tried this but in case I got this prob I would try like this. Assuming label is “content-desc” of node

List elem = driver.findElements(By.className("ExpandableListView "));

for (int i=0;i<elem.size();i++) {
if (elem.get(i).getAttribute(“name”).isEmpty()){
// TO DO CODE what u want to do with element without label
break;
}

}

this sounds good. i’ll try and let you know. Thanks!

at a second look, i don’t think it can work as the attribute is not empty, just different.
also, what should be the condition of ‘for’ ?

  1. List elem = driver.findElements(By.className("ExpandableListView "));
  2. for (int i=0 i less then elem.size() ; i++) {
  3. if(elem.get(i).getAttribute(“name”).isEmpty()) START IF CONDITION Parenthesis
  4. // TO DO CODE what u want to do with element without label
  5. break; // break for loop as we got empty label element
  6. } // END IF CONDITION Parenthesis
  7. } // END FOR LOOP Parenthesis
  8. In line 3 you can put condition for label and it will traverse all child elements of 'ExpandableListView ’ class