Selenium webdriver 无法使用webdriver中的选择在下拉列表中选择选项

Selenium webdriver 无法使用webdriver中的选择在下拉列表中选择选项,selenium-webdriver,drop-down-menu,Selenium Webdriver,Drop Down Menu,我有一个带有以下选项的下拉代码: <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&g

我有一个带有以下选项的下拉代码:

<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>
选择
雇佣的1
雇佣的2
雇佣的3
雇佣4
雇佣的
雇佣的
其他*
我编写了以下代码来查找打印所有选项:

<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。

好的,首先在代码中,代码中不应该有“display:none”。您可以将代码修改为->style=“display:;”>

如果您自己创建html,或者以另一种方式显示下降。第二,打印所有选项

List(WebElement)allSuggestions=driver.findElements(By.id(“SourceOfIncome”)
对于(WebElement suggestion:allSuggestions)
{System.out.println(suggestion.getText());}

好的,首先在代码中,代码中不应该有“display:none”。您可以将代码修改为->style=“display:;”>

如果您自己创建html,或者以另一种方式显示下降。第二,打印所有选项

List(WebElement)allSuggestions=driver.findElements(By.id(“SourceOfIncome”)
对于(WebElement suggestion:allSuggestions)
{System.out.println(suggestion.getText());}

正如您提到的“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());
    }
Select sel=new Select(new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.id(“SourceOfIncome”)));
List options=sel.getOptions();
迭代器Optionitor=options.Iterator();
while(optionistr.hasNext()){
System.out.println(optionistr.next().getText());
}

正如您提到的“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());
    }
Select sel=new Select(new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.id(“SourceOfIncome”)));
List options=sel.getOptions();
迭代器Optionitor=options.Iterator();
while(optionistr.hasNext()){
System.out.println(optionistr.next().getText());
}

你能添加代码吗?你能添加代码吗?