【问题标题】:Selenium - How to read values from drop down based on the value selected in a different drop downSelenium - 如何根据在不同下拉列表中选择的值从下拉列表中读取值
【发布时间】:2015-06-18 11:26:05
【问题描述】:

我有一个具有普通文本字段和下拉菜单的应用程序。 我想根据不同下拉菜单中选定的下拉菜单从下拉菜单中获取数据。为了更清楚, 下拉 1:国家 下拉 2:状态 当我在下拉菜单 1 中选择印度时,印度的所有州都将加载到“州”下拉菜单中。 在这种情况下如何读取“状态”下拉菜单的值。

请告诉我可以用于此的概念。我是编码新手。

问候,

AK

【问题讨论】:

  • 你已经尝试过什么?你能分享你的源代码吗?你能分享一个指向你的应用程序的链接吗?
  • 而且,您需要显示下拉菜单的实际html

标签: java selenium drop-down-menu


【解决方案1】:

为了与 Select WebElements 交互,我建议使用 Selenium 的 Select 类,因为它提供了合适的便捷方法。

您的示例场景可以这样表示:

//Find the Country Select WebElement and select "India"
Select countrySelect = new Select(driver.findElement(By.id("foo")));
countrySelect.selectByVisibleText("India");

//Find the State Select WebElement and retrieve the available Option WebElements
Select stateSelect = new Select(driver.findElement(By.id("bar")));
List<WebElement> stateOptions = stateSelect.getOptions();

//Retrieve the text values of the Option WebElement in the State Select WebElement
List<String> states = new ArrayList<>();
for (WebElement stateOption : stateOptions) {
    states.add(stateOption.getText());
}

选择类 Javadoc: https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/Select.html

【讨论】:

  • 谢谢汤姆。这会有所帮助。问候,AK
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多