This post we will see how to handle Dropdown in Selenium webdriver.
Generally, while working with the script we have to select some values from the dropdown and do other activity and validate. The dropdown is just like simple WebElement like Checkbox, Radiobutton and textbox etc.
Before moving to Dropdown in Selenium webdriver check out two useful plugins that will help you while writing scripts.
Objects or Locators Identification Using Firebug
Select values from Dropdown in Selenium webdriver
For handling dropdowns Selenium already provides Select class that has some predefined method which help is a lot while working with Dropdown.
Select class can be find in below package.
Let’s Discuss some of the method and will see detailed program as well.
In our example we have taken value as March it means it will select March from dropdown.
Note- This is case sensitive it means if I pass march and dropdown has March then Selenium will not be able to select value and it will fail your program so make sure Text which you are passing is correct.
In above example I am passing March in argument so it will select March from dropdown.
Generally, while working with the script we have to select some values from the dropdown and do other activity and validate. The dropdown is just like simple WebElement like Checkbox, Radiobutton and textbox etc.
Before moving to Dropdown in Selenium webdriver check out two useful plugins that will help you while writing scripts.
Select values from Dropdown in Selenium webdriver
For handling dropdowns Selenium already provides Select class that has some predefined method which help is a lot while working with Dropdown.
Select class can be find in below package.
org.openqa.selenium.support.ui.Select
Let’s Discuss some of the method and will see detailed program as well.
Select value using Index.
WebElement month_dropdown=driver.findElement(By.name("DOBMonth"));Here selectbyIndex(int) is method which accept integer as argument so depends on index it will select values. If you give index as 4, it will select 3rd value. Index will start from 0.
Select month=new Select(month_dropdown);
month.selectByIndex(2);
Select value using value attribute.
WebElement month_dropdown=driver.findElement(By.id("month"));Here selectByValue(String) is a method which accept values it means whatever value you have in your dropdown. Please refer below screenshot to see how to get values from dropdown.
Select month=new Select(month_dropdown);
month.selectByValue(“March”);
In our example we have taken value as March it means it will select March from dropdown.
Select value from Visible text
WebElement month_dropdown=driver.findElement(By.id("month"));We can also select value from text as well. This is very straight forward that whatever text we are passing it simply select that value.
Select month=new Select(month_dropdown);
month.selectByVisibleText("March");
Note- This is case sensitive it means if I pass march and dropdown has March then Selenium will not be able to select value and it will fail your program so make sure Text which you are passing is correct.
In above example I am passing March in argument so it will select March from dropdown.
Get Selected option from Dropdown.
WebElement month_dropdown=driver.findElement(By.id("month"));Here getFirstSelectedOption() will return the first selected option and will return you the WebElement then you can use getText() method to extract text and validate the same based on your requirement
Select month=new Select(month_dropdown);
WebElement first_value=month.getFirstSelectedOption();
String value=first_value.getText();