How to select a dropdown value in Selenium WebDriver using Java

JavaSelenium Webdriver

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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");

Solution 2 - Java

If you want to write all in one line try

new Select (driver.findElement(By.id("designation"))).selectByVisibleText("Programmer ");

Solution 3 - Java

As discussed above, we need to implement Select Class in Selenium and further we can use various available methods like :- enter image description here

Solution 4 - Java

Actually select does select but not placing the selected values to the respective field . Where wondered the below snippet works perfectly

driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");

Solution 5 - Java

code to select dropdown using xpath

Select select = new 
Select(driver.findElement(By.xpath("//select[@id='periodId']));

code to select particaular option using selectByVisibleText

select.selectByVisibleText(Last 52 Weeks);

Solution 6 - Java

You can use following methods to handle drop down in selenium.

 1. driver.selectByVisibleText("Text");
 2. driver.selectByIndex(1);
 3. driver.selectByValue("prog");

For more details you can refer http://www.codealumni.com/handle-drop-selenium-webdriver/ this post.

It will definitely help you a lot in resolving your queries.

Solution 7 - Java

WebDriver driver = new FirefoxDriver();
WebElement identifier = driver.findElement(By.id("periodId"));
Select select = new Select(identifier);
select.selectByVisibleText("Last 52 Weeks"); 

Solution 8 - Java

I have not tried in Selenium, but for Galen test this is working,

> > > var list = driver.findElementByID("periodID"); // this will return > web element > > list.click(); // this will open the dropdown list. > > list.typeText("14w"); // this will select option "14w".

You can try this in selenium, the galen and selenium working are similar.

Solution 9 - Java

First Import the package as :

>import org.openqa.selenium.support.ui.Select;

then write in single line as:

>new Select (driver.findElement(By.id("sampleid"))).selectByValue("SampleValue");

Solution 10 - Java

Try this-

driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontestingView Question on Stackoverflow
Solution 1 - JavaAbhishek SinghView Answer on Stackoverflow
Solution 2 - JavaSamKView Answer on Stackoverflow
Solution 3 - JavaAugustRushView Answer on Stackoverflow
Solution 4 - Javauser5027588View Answer on Stackoverflow
Solution 5 - JavaMohan Kumar DgView Answer on Stackoverflow
Solution 6 - JavaJayant GuptaView Answer on Stackoverflow
Solution 7 - JavaAbhishek M SView Answer on Stackoverflow
Solution 8 - JavaajayvView Answer on Stackoverflow
Solution 9 - JavaVishnu MoreView Answer on Stackoverflow
Solution 10 - Javauser4272927View Answer on Stackoverflow