简体   繁体   English

如何获取列表中的所有项目

[英]How to get all items in the list

I need to get all items in the list, but my script shows only the first item. 我需要获取列表中的所有项目,但是我的脚本仅显示第一项。 Please refer to the verifyDisplay assertion in for loop, where I want to show all items in the list. 请参考for循环中的verifyDisplay断言,我要在其中显示列表中的所有项目。 Thanks for the help. 谢谢您的帮助。

My script: 我的剧本:

List<WebElement> groups = driver.findElements(By
            .xpath(".//*[@id='competitiveCategoryTemp']/option"));

    verifyDisplay("'" + competitive_categories_id_with_space + "'" + "===> The newly added Competitive Category is listed",
            By.xpath(".//*[@id='competitiveCategoryTemp']/option"));


    boolean sortedGroups = false;
    for (int i = 0; i < groups.size() - 1; i++) {

        verifyDisplay("'" + groups.get(i).getText() + "'" + "\n"+ 
                "Other Competitive Categories are available and names are SORTED",
                By.xpath(".//*[@id='competitiveCategoryTemp']/option"));

        if (groups.get(i).getText()
                .compareToIgnoreCase(groups.get(i + 1).getText()) > 0) {
            sortedGroups = true;
            break;

        }
        sortedGroups = true;
    }

    if (sortedGroups) {
        System.out.println("The Competitive Category names are SORTED");

    } else
        System.out.println("The Competitive Category names are UN-SORTED");

}
if (groups.get(i).getText()
            .compareToIgnoreCase(groups.get(i + 1).getText()) > 0) {
        sortedGroups = true;
        break;
}

If this condition is met, it breaks out of the for loop, and thus does not proceed forward to the second element. 如果满足此条件,则它将退出for循环,因此不会继续前进到第二个元素。 Could that be the problem? 这可能是问题吗?

WebDriver has Select class to control dropdown objects. WebDriver具有Select类来控制下拉菜单对象。 Your approach will also work. 您的方法也将起作用。 But this way it will look neat and you can reuse the existing methods. 但是这样看起来会很整洁,您可以重用现有方法。

Import this lib. 导入此库。

import org.openqa.selenium.support.ui.Select;

Then, 然后,

Select dropdown = new Select(driver.findElement(By.id("competitiveCategoryTemp")));

dropdown.getOptions()  // will return all the options - it is a List<WebElement>

//To use
for(WebElement option: dropdown.getOptions()){
   System.out.println(option.getText());
}

dropdown.getAllSelectedOptions() // will return the default selected options - it is a List<WebElement>

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

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