Reputation: 1
I have an application which has normal text field and drop down menus. I want to fetch the data from a drop down based on the selected drop down in different drop down menu. To be more clear, Drop Down 1 : Country Drop Down 2 : State When I select India in Drop down 1, all the states in India will be loaded in 'State' drop down. How to read the value of 'State' drop down menu in this scenario.
Please let me know the concept that can be used for this. I am new to coding.
Regards,
AK
Upvotes: 0
Views: 1155
Reputation: 472
For interacting with Select WebElements I would recommend using Selenium's Select class as it provides suitable convenience methods.
Your example scenario could be represented like so:
//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());
}
Select class Javadoc: https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/Select.html
Upvotes: 1