70

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.

3
  • 1
    possible duplicate of stackoverflow.com/questions/9604336/… Commented Nov 22, 2013 at 6:52
  • i tried the linked method it is not working with my options, can i have some other model Commented Nov 22, 2013 at 7:16
  • I think that drop down is not visible for some reason and changing it is not very good approach, but you can always change the element's attribute value using JavaScript Commented Nov 22, 2013 at 18:42

10 Answers 10

149

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");
Sign up to request clarification or add additional context in comments.

5 Comments

This is a fine answer for regular Select usage, but I think the OPs real problem is the fact the the Select element is not visible - <select id="periodId" name="period" style="display: none;">
By the way the import is : import org.openqa.selenium.support.ui.Select;
For those using C# note you have to installed "Selenium Support" available in NuGet in order to use this solution. Also in C# it's called SelectElement instead of Select.
If you know the value to be selected, then even driver.findElement(By.xpath(Your_Xpath_to_Element)).sendKeys("Selected Value ") - Tested with FireFox and working good
@RajeshBalan. Yes. However identifying elements by XPath is not advisable. Automation code will fail if DOM structure changes.
5

If you want to write all in one line try

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

Comments

5

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

Comments

3

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

1 Comment

It did not work for me. I used: driver.findElement(By.xpath("//select[option[@value='50']]")).sendKeys("50");
2

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.

Comments

0

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

Comments

-1

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.

Comments

-1

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

Comments

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

2 Comments

Please explain your answer.
While this code may provide a solution to problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution.
-3

Try this-

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.