【问题标题】:Is there any other option to select item from dropdown instead "selectByVisibleText"?是否有任何其他选项可以从下拉列表中选择项目而不是“selectByVisibleText”?
【发布时间】:2016-09-08 15:27:15
【问题描述】:

我的 Selenium 测试中有以下代码行是关于从 DropDown 中选择项目的:

new Select(driver.findElement(By.cssSelector("select[id='application_id']")).selectByVisibleText("NewApp");

还有其他选项可以从下拉列表中选择项目吗?

比如 CSS 选择器。

【问题讨论】:

    标签: selenium webdriver css-selectors selenium-webdriver


    【解决方案1】:

    是 - 请参阅从 WebDriver API documentation 中选择:您还可以使用 selectByIndex(int index)selectByValue(java.lang.String value) 按索引和值进行选择

    【讨论】:

      【解决方案2】:

      没有规定必须使用Select 类来处理<select> 元素。例如,您可以执行以下操作:

      WebElement element = driver.findElement(By.cssSelector("select[id='application_id']"));
      WebElement option = element.findElement(By.cssSelector("optionAttribute"));
      option.click();
      

      请注意,您可以使用此技术通过任何标准 By 方法找到您想要的选项。

      【讨论】:

      • 谢谢。好吧,我是 Java 和 Selenium 的新手,所以我使用 Selenium IDE,然后编辑脚本。这就是为什么我认为下拉菜单只有我上面提到的方法是有效的。所以,谢谢你的解释。
      【解决方案3】:

      试试这个:

      WebElement element = driver.findElement(By.cssSelector("select[id='application_id']"));
      element.sendKeys("value_with_you_want_to_select");
      

      【讨论】:

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