I'm trying to get the items from a ListView so I can compare them with of expected results. The thing is that I can't figure out how to get those Elements.

I'm also the developer of the App, so I'm using the ID that I assigned to that element to identify it.

Here is the code that I have right now:

public String playersListItem(int n){
    List<WebElement> players = driver.findElements(By.id("playersList"));
    return players.get(n).getText();

I'm comparing playersListItem(0) with "Jones" but the item in the position 0 is always blank.

this is the reason of the fail that I get in the Assert:

java.lang.AssertionError: expected [Jones] but found []


2 Answers 2


There should only be one element with an ID of "playersList" so you won't get a set of elements from that item. You first need to grab the outside element, the one with ID and then grab the elements inside that element. Assuming that this is an un-ordered list it would look like this

public String playersListItem(int n){
    WebElement ulElement = driver.findElement(By.id("playersList"));
    List<WebElement> listOfPlayers = ulElement.findElements(By.tagName("li"));
    return players.get(n).getText();

To get all the TextViews from a list you can do something like this:

List<AndroidElement> textViews = ad.findElementsByClassName("android.widget.TextView");
            for (WebElement textView : textViews) {
                        return textView.getText();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.