简体   繁体   中英

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. 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. Could that be the problem?

WebDriver has class to control dropdown objects. 类来控制下拉菜单对象。 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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