我怎样才能 select 列表中的项目

How can i select items from the list

我无法获取列表中的项目。我得到的只是跨度中的一个项目。如何获得 select 中的项目。如果我不在 css selector 中使用 span,那么脚本会生成一个错误,指出该元素不可见。

这是页面的HTML:

<table>
<tbody>
    <tr>
        <td>
            <div>
                <div class="select2-container select2-container-active" id="s2id_agency" style="width: 150px;">
                <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1">   
                <span>01_me</span>
                <abbr class="select2-search-choice-close" style="display:none;"></abbr>   
                <div>
                <b></b>
                </div>
                </a>
                <input class="select2-focusser select2-offscreen" type="text"></div>
                <select name="agency" id="agency" style="width:150px;" class="select2-offscreen" tabindex="-1">
                <option value="14814">01_me</option>
                <option value="14333">A_RLAgency</option>
                <option value="14771">Agency_20150111183734</option>
                <option value="14254">RLAgency</option>
                <option value="14065">unknown_agency</option></select>
            </div>
        </td>
    </tr>   
</tbody>

这是我的脚本:

        try {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        List<String> displayed = new ArrayList<String>();
        List<String> sorted = new ArrayList<String>();

        List<WebElement> verifyAgency = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By
                .cssSelector("#s2id_agency > a.select2-choice > span")));


        System.out.println("NUMBER OF ROWS IN THIS TABLE = "
                + verifyAgency.size());


        for (WebElement element : verifyAgency) {
            System.out.println("" + element.getText());
            displayed.add(element.getText());
            sorted.add(element.getText());
        }

        Collections.sort(sorted);
        log.info(sorted);
        if (!displayed.equals(sorted)) {

            final String failedMsg = "Agency is not in alphabatical ordr.";
            log.error(failedMsg, null);
            boolean passed = false;
            Assert.assertFalse(passed, failedMsg);
            System.out.println(failedMsg);
        }

    } catch (Exception e) {

        final String failedMsg = "Failed trying to check agency names in alphabetical order.";
        log.error(failedMsg, null);
        boolean passed = false;
        Assert.assertFalse(passed, failedMsg);
        System.out.println(failedMsg);
    }

您可能试图在 select 列表选项上使用 visibilityOfAllElementsLocatedBy,但这些选项不可见。事实上,根据您的要求,您不需要它们可见。您也可以使用 presenceOfElementLocated 并获取简单的列表 SelectElement class 就足够了

注:我的是C#

    IWebElement element = driver.FindElement(By.XPath("//select[@id='agency']"));

    SelectElement selectList = new SelectElement(element);
    IList<IWebElement> options = selectList.Options;

而且,如果这是一个静态列表,您可以避免所有这些古怪的工作,只需执行

    IWebElement element = driver.FindElement(By.XPath("//*[@id='agency']"));    
    SelectElement selectList = new SelectElement(element);

    var isTrue = selectList.getText().equals("01_me / A_RLAgency / Agency_20150111183734 / RLAgency / unknown_agency")

尝试使用此代码:

Select objSelect = new Select(dw2.findElement(By.id("agency")));
List<WebElement> lstOptions = objSelect.getOptions();
boolean sorted = false;
for (int i = 0; i < lstOptions.size() - 1; i++) {
    if (lstOptions.get(i).getText()
        .compareTo(lstOptions.get(i + 1).getText()) > 0) { // Checking sorting based on natural sort
    sorted = false;
    break;
    }
    sorted = true;
}
if (sorted)
    System.out.println("Sorted");
else
    System.out.println("Un-sorted");