【问题标题】:How to select value from search drop down如何从搜索下拉列表中选择值
【发布时间】:2020-10-16 10:21:20
【问题描述】:

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

// 从下拉列表中选择值

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

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

请考虑下图以获得更好的理解。 click this Image Link

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

【问题讨论】:

  • 你能分享你的html吗?我猜在源代码中不是<selector> 标签,所以new Select() 不会工作。
  • 也请分享 html。
  • 你需要显示下拉菜单的 HTML
  • 是否在<li>标签中显示下拉元素?

标签: selenium selenium-webdriver


【解决方案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();

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 您能否发送一些有关错误的日志或一些信息。 webelement 属性的屏幕截图也会有所帮助。
    【解决方案3】:

    您可以使用 SelectByValue 方法。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多