簡體   English   中英

硒-如何基於不同下拉菜單中選擇的值從下拉菜單中讀取值

[英]Selenium - How to read values from drop down based on the value selected in a different drop down

我有一個具有普通文本字段和下拉菜單的應用程序。 我想根據其他下拉菜單中的選定下拉列表從下拉列表中獲取數據。 更清楚地說,下拉列表1:國家下拉列表2:州在下拉列表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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM