Selenium - 无法从下拉列表中选择 select 选项

Selenium - Not able to select option from drop down

代码如下:

public static void test1() {  
System.out.print("\nTo find UserName element");
Select select = new Select(driver.findElement(By.id("drop_down")));
System.out.print("\nElements found");
select.selectByIndex(1);
}

以下均未解决:

select.selectByIndex(1);
select.selectByValue("1");
select.selectByVisibleText("Super Admin"); 

它抛出一个异常: 线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:找不到值为 1

的选项
<select id="drop_down" style="width:205px;" name="drop_down">
    <option value=""></option>
    <option value="1">
        Super Admin
    </option>
    <option value="4">
        Question Reviewer
    </option>
    <option value="6">
    Evaluator
    </option>
</select>

可能是当您尝试访问下拉菜单时未正确加载它

尝试下面的代码等到下拉列表中的选项数大于 1,然后select第一个选项:

try{
    // Waits for 20 seconds
    WebDriverWait wait = new WebDriverWait(driver, 20);

    // Wait until expected condition size of the dropdown increases and becomes more than 1
    wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>(){
        public Boolean apply(WebDriver driver)  
        {
            Select select = new Select(driver.findElement(By.id("drop_down")));
            return select.getOptions().size()>1;
        }
    });

    //To select the first option
    Select select = new Select(driver.findElement(By.id("drop_down")));
    select.selectByVisibleText("Super Admin");
}catch(Throwable e){
    System.out.println("Error found: "+e.getMessage());
}

您好,根据 1 月 15 日 Bu Subh 在 6:35 发表的第一条评论,我也修改了代码,但得到的错误与 Abhinav 于 1 月 15 日在 6:53 提到的相同,之后 Subh 说 "I have edited my code above.. See if it works out for you and let me know too, please.." 但我在评论后没有看到任何修改过的代码,因此这没有帮助....最后我搜索了几个其他论坛,我尝试使用 selectByIndex() as:-

WebElement toactTyp=driver1.findElement(By.name((<Name of the Element to access>)));
Select toactSel=new Select(toactTyp);
toactSel.selectByIndex(2);

它与上面的代码配合得很好.....我请求分享修改后的代码或至少修改完成的行,因为它对像我这样的很多人有用