簡體   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