66

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
testing
  • 1,736
  • 15
  • 46
  • 75
  • 1
    possible duplicate of http://stackoverflow.com/questions/9604336/selenium-webdriver-select-element – dckuehn Nov 22 '13 at 06:52
  • i tried the linked method it is not working with my options, can i have some other model – testing Nov 22 '13 at 07: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 – Furious Duck Nov 22 '13 at 18:42

10 Answers10

142

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");
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
  • 2
    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 - – Steve Weaver Crawford Nov 22 '13 at 18:57
  • 5
    By the way the import is : import org.openqa.selenium.support.ui.Select; – sinisterrook Apr 16 '15 at 18:48
  • 32
    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`. – Luis Perez Oct 19 '15 at 18:35
  • 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 – Rajesh Balan Aug 31 '16 at 18:43
  • @RajeshBalan. Yes. However identifying elements by XPath is not advisable. Automation code will fail if DOM structure changes. – Abhishek Singh Jul 25 '17 at 07:53
5

If you want to write all in one line try

new Select (driver.findElement(By.id("designation"))).selectByVisibleText("Programmer ");
SamK
  • 377
  • 9
  • 27
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

AugustRush
  • 367
  • 4
  • 4
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");
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
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.

Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
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);
Community
  • 1
  • 1
-1
WebDriver driver = new FirefoxDriver();
WebElement identifier = driver.findElement(By.id("periodId"));
Select select = new Select(identifier);
select.selectByVisibleText("Last 52 Weeks"); 
John Smith
  • 7,243
  • 6
  • 49
  • 61
  • 2
    Please explain your answer. – marian0 Oct 21 '15 at 07:20
  • 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. – palaѕн May 15 '20 at 04:50
-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.

Community
  • 1
  • 1
ajayv
  • 641
  • 6
  • 21
-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");

Vishnu More
  • 45
  • 1
  • 12
-3

Try this-

driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");
John Smith
  • 7,243
  • 6
  • 49
  • 61