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

时间:2017-05-29 09:31:12

标签: selenium selenium-webdriver

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

//从下拉列表中选择值

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

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

请考虑以下图片以便更好地理解。 click this Image Link

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

3 个答案:

答案 0 :(得分:1)

    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();

答案 1 :(得分:0)

检查下拉列表找到值并选择您选择的选项。

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");

答案 2 :(得分:0)

您可以使用SelectByValue方法。

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

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