选择标记为列表的下拉列表中的元素

时间:2015-09-20 21:28:51

标签: javascript html selenium selenium-webdriver

我试图遍历下拉框中的所有元素...但是与具有值的典型html框不同,这些都是UL / LI。这是html的样子:

<ul class="ui-selectmenu-menu ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom jfpw-select ui-selectmenu-open" aria-hidden="false" role="listbox" aria-labelledby="currentStatementsDate-button" id="currentStatementsDate-menu" aria-activedescendant="currentStatementDateOptions" tabindex="0" style="width: 328px; height: 160px; float: left; overflow: hidden; outline: none; z-index: 0; top: 0px; left: 0px; position: relative; border-style: none;">
   <li role="option" tabindex="-1" aria-selected="false" id="currentStatementDateOptions" class=""><span class="ui-selectmenu-item-header">Unbilled</span></li>
   <li role="option" tabindex="-1" aria-selected="true" id="currentStatementDateOptions" class="ui-selectmenu-item-selected"><span class="ui-selectmenu-item-header">September 10, 2015</span></li>
   <li role="option" tabindex="-1" aria-selected="false" id="currentStatementDateOptions" class=""><span class="ui-selectmenu-item-header">August 12, 2015</span></li>
   <li role="option" tabindex="-1" aria-selected="false" id="currentStatementDateOptions" class=""><span class="ui-selectmenu-item-header">July 10, 2015</span></li>
   <li role="option" tabindex="-1" aria-selected="false" id="currentStatementDateOptions" class=""><span class="ui-selectmenu-item-header">June 10, 2015</span></li>
   <li role="option" tabindex="-1" aria-selected="false" id="currentStatementDateOptions" class="ui-corner-bottom"><span class="ui-selectmenu-item-header">May 12, 2015</span></li>
</ul>

我需要以某种方式选择每个列表项......但由于这些与典型的下拉列表不同,我不确定。

我猜我需要为每次交互更改属性(aria-selected)和类(所选),但我不确定如何继续,因为它们都具有相同的id。

2 个答案:

答案 0 :(得分:1)

我会找到WebElements列表并使用nt-child增加nth-child()函数

#currentStatementsDate-menu>li:nth-child(1)

这样的事情:

By css = By.cssSelector("#currentStatementsDate-menu>li");
List<WebElement> elements = driver.findElements(css);

for (int i = 0; i < elements.size(); i++) {
    By by = By.cssSelector(String.format("#currentStatementsDate-menu>li:nth-child(%s)", i+1));
    driver.findElement(by).click();
}

答案 1 :(得分:0)

也许您可以使用jquery遍历元素:

var test = $("#currentStatementsDate-menu").children().each(function() {
    var li_element = $(this);
    console.log(li_element);
});

另外,我不确定具有所有相同ID的li元素是否正确。