如何从列表中选择项目

时间:2015-01-13 22:06:30

标签: selenium selenium-webdriver

我无法获取列表中的项目。我得到的只是一个跨越的项目。如何在选择中获取项目。如果我不在css选择器中使用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);
    }

2 个答案:

答案 0 :(得分:0)

您可能尝试在不可见的选择列表选项上使用visibilityOfAllElementsLocatedBy。事实上,根据您的要求,您不需要它们可见。您也可以使用presenceOfElementLocated 并且要抓住列表,简单的SelectElement类就足够了

注意:我的是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")

请参阅here

答案 1 :(得分:0)

尝试使用此代码:

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");