Selenium 如何从“搜索”下拉列表中选择值

Selenium 如何从“搜索”下拉列表中选择值,selenium,selenium-webdriver,Selenium,Selenium Webdriver,如何从此类下拉列表中选择值 //从下拉列表中选择值 Select dropdown = new Select(driver.findElement(By.id("select2-operative_id-container"))); dropdown.selectByVisibleText("Administrator"); 及 尝试以上两种方法,但无法选择值 请考虑下面的图片以便更好地理解。 对于下拉列表值,我无法选择Xpath。检查下拉列表,找到值并选择所选选项 Select dro

如何从此类下拉列表中选择值

//从下拉列表中选择值

 Select dropdown = new Select(driver.findElement(By.id("select2-operative_id-container"))); 
dropdown.selectByVisibleText("Administrator");

尝试以上两种方法,但无法选择值

请考虑下面的图片以便更好地理解。


对于下拉列表值,我无法选择Xpath。

检查下拉列表,找到值并选择所选选项

Select dropdown = new Select(driver.findElement(By.id("select2-operative_id-container")));

dropdown.selectByValue("Administrator");
或者另一种方法是发送键来选择选项

WebElement dropdown = new WebElement(driver.findElement(By.id("select2-operative_id-container")));
dropdown.sendKeys("Administrator");

您可以使用SelectByValue方法

将值名称添加为字符串,如下所示

Select oSelect = new Select(driver.findElement(By.id("select2-operative_id-container")));
oSelect.selectByValue(<your value>);
Select oSelect=new Select(driver.findElement(By.id(“select2-operative\u id-container”));
oSelect.selectByValue();

你能分享你的html吗?我猜源代码中没有
标记,因此
新选择()
无法工作。也请共享html。您需要显示下拉列表的html是否在
  • 标记中显示下拉列表元素?能否发送一些日志或有关错误的信息。webelement属性的屏幕截图也会有所帮助。
        This is select drop down.
    
        Select dropdown = new Select(driver.findElement(By.xpath("put here xpath")));
        dropdown.selectByValue("A");
    
        For bootstrap dropdown
    # you have to first click on the arrow icon(v) of the drop down.
      driver.findElement(By.xpath("put here xpath of the v icon")).click();
    # Then find the xpath of the value which you have to select from drop down and then apply click operation on it.
    driver.findElement(By.xpath("put here xpath of the value within drop down")).click();