繁体   English   中英

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

[英]How to get all items in the list

我需要获取列表中的所有项目,但是我的脚本仅显示第一项。 请参考for循环中的verifyDisplay断言,我要在其中显示列表中的所有项目。 谢谢您的帮助。

我的剧本:

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

如果满足此条件,则它将退出for循环,因此不会继续前进到第二个元素。 这可能是问题吗?

WebDriver具有Select类来控制下拉菜单对象。 您的方法也将起作用。 但是这样看起来会很整洁,您可以重用现有方法。

导入此库。

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

然后,

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