无法在下拉列表中获得扩展选项

时间:2017-03-24 12:44:15

标签: java jquery selenium select selenium-webdriver

我想从选择按钮获取所有选项。

<select id="filterSel" name="filterSel" class="fixed-size" onchange="fnLoadAccountslegEnt(this.value); " onclick="closeDropDown();" size="1" style="top: 6.5px; margin: 0px; width: 58%; height: 30px; z-index: 9999; background: rgb(255, 255, 255);">                                 
  <option value=""></option>
  <option value="aa" title="abcd">abcd</option>
  <option value="bb" title="xyz">xyz</option>
  <option value="More" style="color: blue;">More...</option>
</select>

第三个选项是&#34;更多......&#34;按此更多选项加载到下拉列表中。即,现在只有更多选项可见。我需要使用selenium WebDriver访问所有选项。使用以下代码,我只能获得在控制台上打印的选项标签中已有的内容...打印的最后一个选项是&#34;更多...&#34;。 我的代码:

Select select=new Select(element_select);

List<WebElement> options = select.getOptions();
int i=1;
for(WebElement ele:options) {
    if(str.contains("More")) {  
        Filter.FilterApplied().sendKeys(str);
        Filter.FilterApplied().click();
    }
    str=ele.getText();
    System.out.println("options are :"+str);
    i++;
}    

错误讯息:

  

FAILED:main org.openqa.selenium.WebDriverException:未知错误:   元素...在点上无法点击   (262,84)。其他元素将收到点击:

2 个答案:

答案 0 :(得分:0)

您可以使用JSTL for循环动态填充下拉列表。 1.添加一个动作        <option value="More" onclick="//goToSomeController" style="color: blue;"><%= //could be an array of options %></option>

从那里你可以用选项填充循环和数组的大小。希望有所帮助。

答案 1 :(得分:0)

您可以使用Select类选择more选项并加载所有选项

Select select = new Select(element_select);
List<WebElement> options = select.getOptions();
int size = options.size();

select.selectByValue("More");    
while ((options = select.getOptions()).size() == size);

int i = 1;
for(WebElement ele : options)
{
    str = ele.getText();
    System.out.println("options are :"+str);
    i++;
}

while ((options = select.getOptions()).size() == size);将等待更多选项加载,只有在您确定有更多选项时才会以这种方式使用它,否则您将最终无限循环。

相关问题