我有一个下拉代码,其中包含以下选项:
<select class="col-10 customDropdown focusOutVal xh-highlight" id="SourceOfIncome" name="PrimaryIncome.SourceOfIncome" sb="59436885" style="display: none;"><option value="" class="" selected="selected">Select</option>
<option value="Employed1" class="">Employed1</option>
<option value="Employed2" class="">Employed2</option>
<option selected="Employed3" value="Employed3" class="">Employed3</option>
<option value="Employed4" class="" selected="selected">Employed4</option>
<option value="Employed5" class="">Employed5</option>
<option value="Employed6" class="">Employed6</option>
<option value="Other" class="">Other*</option>
</select>
我编写了以下代码来查找打印所有选项:
我为所有元素获得相同的值,当我使用visible-text选项选择特定元素时,它给出了ElementnotVisibleException。
答案 0 :(得分:0)
好的,首先在代码中,代码中不应该有“display:none”。您可以将代码修改为 - &gt; style =“display:;”&gt;
如果您自己创建html,或者下垂以另一种方式显示。其次,打印所有选项
List(WebElement)allSuggestions = driver.findElements(By.id(“SourceOfIncome”));
for(WebElement建议:allSuggestions)
{的System.out.println(suggestion.getText());}
答案 1 :(得分:0)
当你提到“ElementnotVisibleException”时,它似乎是一个等待问题。考虑到您尚未在代码中引入WebDriverWait,请尝试按照
Select sel = new Select(new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.id("SourceOfIncome"))));
List<WebElement> options = sel.getOptions();
Iterator<WebElement> optionItr = options.iterator();
while(optionItr.hasNext()){
System.out.println(optionItr.next().getText());
}