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

Select Selenium-无法从下拉列表中选择选项,select,selenium,drop-down-menu,Select,Selenium,Drop Down Menu,代码如下: 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(

代码如下:

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,然后从中选择第一个选项:

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());
}
试试看{
//等待20秒
WebDriverWait wait=新的WebDriverWait(驱动程序,20);
//等待下拉列表的预期条件大小增加并超过1
等待.直到((ExpectedCondition)新的ExpectedCondition(){
公共布尔应用(WebDriver驱动程序)
{
Select=newselect(driver.findElement(By.id(“下拉”));
返回select.getOptions().size()>1;
}
});
//选择第一个选项
Select=newselect(driver.findElement(By.id(“下拉”));
select.selectByVisibleText(“超级管理员”);
}捕获(可丢弃的e){
System.out.println(“发现错误:+e.getMessage());
}

您好,根据1月15日6:35的第一条评论,我也修改了代码,但在该副标题说“我已经编辑了上面的代码……看看它是否适合您,请让我也知道”后,得到了与Abhinav在1月15日6:53提到的相同的错误但我在这条评论后没有看到任何修改过的代码,因此这没有帮助……最后我搜索了几个其他论坛,并尝试使用
selectByIndex()
作为:-

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

它与上面的代码配合得很好…..我请求共享修改过的代码或至少修改过的行,因为这对我这样的人很有用

下拉列表的值是根据输入的电子邮件填充的。仍然收到相同的错误:无法用文本定位元素:超级管理员我必须选择超级管理员。好的。。在输入电子邮件地址之前,您是否可以添加下拉列表的HTML片段,正如您所说的“下拉列表的值是根据输入的电子邮件填充的”?然后我会相应地修改我的代码,也选择角色,在上面的html片段中的名称就像是两年后的drop_down[index]。很抱歉反应太晚……)实际上我修改了代码。早些时候,它只是等待下拉列表出现,然后选择相关选项。OP回复后,确认该应用程序正在使用ajax加载选项作为输入的电子邮件地址。因此,我修改了代码,直到选项计数>1,然后
selectByVisibleText
,在这里我可以使用
selectByindex
,并给出同样有效的“1”。出于正常目的,您的代码是预期的,当然,因为您的代码是下拉选项恒定的情况。