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

问题描述 投票:1回答:3

如何从这种类型的下拉列表中选择值。

//从下拉列表中选择值

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

尝试以上两种方法但不能选择值。

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

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

selenium selenium-webdriver
3个回答
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();

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

0
投票

您可以使用SelectByValue方法。

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

Select oSelect = new Select(driver.findElement(By.id("select2-operative_id-container")));
oSelect.selectByValue(<your value>);
© www.soinside.com 2019 - 2024. All rights reserved.