Home > Blockchain >  List<WebElement> returns empty List
List<WebElement> returns empty List

Time:10-23

I am trying to loop over a List and store the items in another List to compare data, but my List is not getting iterated over,

Here is my implementation

    private final By listNotificationType = By.xpath("//*[@id='a24687a9017f']//p/text()");
   
    public List<WebElement> verifyListNotificationType()
        {
            List<WebElement> drpdwnData = new ArrayList<>();
            for(WebElement a: driver.findElements(listNotificationType));
            {
                drpdwnData.add(a);
            }
            return drpdwnData;
        }
        
    String[] arrNotifications = { "Abc", "Xyz", "Def" };
    List<Object> listNotifications = Arrays.asList(arrNotifications);
    
    MyPage myPage = new MyPage();
    System.out.println("Final Data:: " myPage.verifyListNotificationType());         
  Assertions.assertThat(listNotifications).hasSameElementsAs(myPage.verifyListNotificationType());

What happens when I execute the code is I get the result Final Data:: []

Can someone please tell me why the list is returning empty, my xpath is accurate as I rechecked it but still I am unable to iterate over it, while debugging the for loop is not even getting executed. I am not sure where I am going wrong.

CodePudding user response:

Instead of

List<WebElement> drpdwnData = new ArrayList<>();

you should define list of WebElement like this

List<WebElement> drpdwnData = new ArrayList<WebElement>();

Also, We do not need ; for for loop declaration. Please see this line :

for(WebElement a: driver.findElements(listNotificationType));

I would also suggest not using text() in XPath. Instead, try with the below code.

You should also put a if condition, before iterating, If the size of list >0 then go inside for loop, otherwise do not enter inside loop. In that way you will have bit optimized code.

Code:

private final By listNotificationType = By.xpath("//*[@id='a24687a9017f']//p");

public List<WebElement> verifyListNotificationType()
{
    List<WebElement> drpdwnData = new ArrayList<WebElement>();
    List<WebElement> actualList = driver.findElements(listNotificationType);
    if (actualList.size() > 0) {
        System.out.println("actualList does have at least one web element, So Bot will go inside loop.");
        for(WebElement a : actualList)
            drpdwnData.add(a);
    }
    else
        System.out.println("actualList does not have any web element, So Bot will not go inside loop.");
    return drpdwnData;
}

CodePudding user response:

Capture the WebelEments before loop begins.

My xpath is accurate as I rechecked it but still I am unable to iterate over it, while debugging the for loop is not even getting executed.

May be returning 0 elements so use some wait statement.

private final By listNotificationType = By.xpath("//*[@id='a24687a9017f']//p");
WebDriverWait wait = new WebDriverWait(driver, 20);
List<WebElement> actualList = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(listNotificationType));

public List<WebElement> verifyListNotificationType() {
    List<WebElement> drpdwnData = new ArrayList<>();
    for (WebElement a : actualList) {
        drpdwnData.add(a);
    }
    return drpdwnData;
}
  • Related