Search code examples
javaseleniumcollectionsiteratorappium

Converting List<WebElement> to WebElement


I am using Appium and I want to print names of the elements in the list. I am using following code

List<WebElement> list = getDriver().findElementsByXPath(getLocator(Locators.MY_ITEM));
    List<String> strings = new ArrayList<>();
    for (WebElement object : list) {
        String text = object.getText();
        logger.info(text);
        if (!text.isEmpty())
            strings.add(text);
    }

But I am getting text always as empty. What is the suggested approach over here. Note each element is of type UIACollectionCell in case of iOS and on Android //android.widget.TextView[@text='%s']


Solution

  • From what I understand, you should be getting the text from the text attribute, replace:

    String text = object.getText();
    

    with:

    String text = object.getAttribute("text");