0

朋友们,

我有一种情况,我需要单击下拉列表并选择显示的任何值。下拉列表由以下代码段标识

<select id="order_unit_line_rate_806782_is_addenda_enabled" class="selects_for_487886" onchange="select_addendum(806782, this);dateShowMemory(this.options[this.selectedIndex].value, '806782');" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / drop down" name="order_unit_line_rate[806782][is_addenda_enabled]">
    <option value="0" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Fee"> Fee </option>
    <option value="1" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / See Attached Addendum"> See Attached Addendum </option>

其中“选择”和“选项”标签位于嵌套层次结构中。我可以单击下拉列表并通过这样做来显示项目

List<WebElement> dropDownLists = driver.findElements(By.tagName("select"));
for (WebElement l : dropDownLists) { 
    if (l.getAttribute("uniqueattr").equalsIgnoreCase("Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / drop down")) {
              l.click();
    } // end if
} // end for

但是我无法进一步遍历以单击下拉列表中的选项:(。

这是我尝试过的,但不起作用

List<WebElement> newList = driver.findElements(By.tagName("option"));
for (WebElement ll : newList) {
    if (ll.getAttribute("uniqueattr").equalsIgnoreCase("Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Straight Line Commitment")) {
        ll.click();
    }
}
4

2 回答 2

0

Thats what the Select is for..

do something like:

    Select dropDown = new Select(dropDownElement);
    for (WebElement option : dropDown .getOptions()){
        if(!option.isSelected()){
            option.click();
        }
    }
于 2012-07-05T12:12:23.183 回答
0

检索您要点击的选项的标签、索引或值,如下所示

List<WebElement> dropDownLists = driver.findElements(By.tagName("select"));
for (WebElement l : dropDownLists) 
{ 
    List<WebElement> newList = l.findElements(By.tagName("option"));
    for (WebElement ll : newList) 
    {
            if (ll.getAttribute("uniqueattr").equalsIgnoreCase("Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Straight Line Commitment")) 
            {
                SelectElement select = new SelectElement(l);
                select.SelectByValue(ll.getAttribute("value"));
            }
    }
} 
于 2012-07-05T11:44:22.170 回答