简体   繁体   English

列表<WebElement>返回空列表

[英]List<WebElement> returns empty List

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,我试图遍历一个 List 并将项目存储在另一个 List 中以比较数据,但我的 List 没有被迭代,

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:: []当我执行代码时会发生什么我得到了结果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.有人可以告诉我为什么列表返回空,我的 xpath 在我重新检查时是准确的,但我仍然无法迭代它,而调试 for 循环甚至没有被执行。 I am not sure where I am going wrong.我不确定我哪里出错了。

Instead of代替

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

you should define list of WebElement like this你应该像这样定义 WebElement 列表

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

Also, We do not need ;此外,我们不需要; for for loop declaration. for for 循环声明。 Please see this line :请看这一行:

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

I would also suggest not using text() in XPath.我还建议不要在 XPath 中使用text() 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.您还应该在迭代之前放置一个if条件,如果列表的大小>0则进入for循环,否则不要进入循环。 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;
}

Capture the WebelEments before loop begins.在循环开始之前捕获WebelEments

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.当我重新检查它时,我的 xpath 是准确的,但我仍然无法对其进行迭代,而调试 for 循环甚至没有被执行。

May be returning 0 elements so use some wait statement.可能会返回0元素,因此请使用一些等待语句。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM