Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a dropdown value in Selenium WebDriver using Java

I am new to selenium , currently am working on selenium webdriver i want to select a value from the drop down. The id=periodId and the option is many in that am trying to select Last 52 weeks.

Here is the HTML code:

<select id="periodId" name="period" style="display: none;">     <option value="l4w">Last 4 Weeks</option>     <option value="l52w">Last 52 Weeks</option>     <option value="daterange">Date Range</option>     <option value="weekrange">Week Range</option>     <option selected="" value="monthrange">Month Range</option>     <option value="yeartodate">Year To Date</option> </select> 

Please suggest me some ways to click the drop down.

I tried with the above example lines but am getting error such as Element is not currently visible and so may not be interacted with Command duration or timeout: 32 milliseconds the drop downs values are the jquery multiselect widget format.

like image 691
testing Avatar asked Nov 22 '13 06:11

testing


People also ask

How do I select a specific value from a dropdown?

selectByValue(): You can select an option by using Value attribute provided for each option in dropdown menu. So you can use this Value to select any particular option: WebElement element = driver. findElement(By.id("year")); Select select = new Select(element); select.

How does Selenium check selected dropdown value?

We can get a selected option in a dropdown in Selenium webdriver. The method getFirstSelectedOption() returns the selected option in the dropdown. Once the option is fetched we can apply getText() method to fetch the text.


1 Answers

Just wrap your WebElement into Select Object as shown below

Select dropdown = new Select(driver.findElement(By.id("identifier"))); 

Once this is done you can select the required value in 3 ways. Consider an HTML file like this

<html> <body> <select id = "designation"> <option value = "MD">MD</option> <option value = "prog"> Programmer </option> <option value = "CEO"> CEO </option> </option> </select> <body> </html> 

Now to identify dropdown do

Select dropdown = new Select(driver.findElement(By.id("designation")));

To select its option say 'Programmer' you can do

dropdown.selectByVisibleText("Programmer ");

or

dropdown.selectByIndex(1);

or

 dropdown.selectByValue("prog"); 
like image 71
Abhishek Singh Avatar answered Sep 20 '22 10:09

Abhishek Singh