43

If I want to select an option of a dropdown box, there are several ways to do that. I always used:

driver.findElement(By.id("selection")).sendKeys("Germany");

But that didn't work every time. Sometimes another option was selected. So I googled a little bit and found this piece of code which works every time:

WebElement select = driver.findElement(By.id("selection"));
    List<WebElement> options = select.findElements(By.tagName("option"));
    for (WebElement option : options) {
        if("Germany".equals(option.getText()))
            option.click();
    }

But that works really really slow. If I have a long list with many items in it, it really takes too much time. So my question is, is there a solution which works every time and is fast?

franklin
  • 1,770
  • 6
  • 30
  • 58
tester
  • 3,897
  • 5
  • 35
  • 59

10 Answers10

50

You could try this:

IWebElement dropDownListBox = driver.findElement(By.Id("selection"));
SelectElement clickThis = new SelectElement(dropDownListBox);
clickThis.SelectByText("Germany");
Michael Bautista
  • 1,210
  • 12
  • 19
  • 25
    I think this is some C# code or something? But it helped me to figure out the following code: WebElement dropDownListBox = driver.findElement(By.id("selection")); Select clickThis = new Select(dropDownListBox); clickThis.selectByValue("Germany"); A lot faster! Thanks! – tester Aug 30 '11 at 09:40
  • 2
    Which package should be imported for IWebElement and SelectElement? – Ripon Al Wasim Jul 26 '12 at 08:21
  • 1
    Where can I find the SelectElement class? – amhed May 07 '13 at 21:23
  • 2
    Note in C# `SelectElement` is part of a different NuGet project called Selenium.Support. – Luis Perez Oct 19 '15 at 18:38
25

Try the following:

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

Select droplist = new Select(driver.findElement(By.Id("selection")));   
droplist.selectByVisibleText("Germany");
Ripon Al Wasim
  • 35,466
  • 40
  • 150
  • 172
StatusQuo
  • 1,396
  • 1
  • 12
  • 13
4

Try the Select helper class and see if that makes any difference?

String valueToSelect= "Germany";
WebElement select = driver.findElement(By.id("selection"));
Select dropDown = new Select(select);           
String selected = dropDown.getFirstSelectedOption().getText();
if(selected.equals(valueToSelect)) {//do stuff already selected}
List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
  if(option.getText().equals(valueToSelect)){
       option.click();  
  }
}
nilesh
  • 13,751
  • 6
  • 62
  • 77
2

For some strange reason the SelectElement for webdriver (version 2.25.1.0) does not properly work with the firefoxdriver (Firefox 15). Sometimes it may not select an option from a dropdownlist. It does, however, seem to work with the chromedriver... This is a link to the chromedriver... just drop it in the bin dir.

Jainendra
  • 23,989
  • 30
  • 120
  • 167
Skynet
  • 67
  • 7
1

Example for select an option from the drop down list:

Click on drop down list by using id or csspath or xpath or name. I have used id here.

driver.findElement(By.id("dropdownlistone")).click(); // To click on drop down list
driver.findElement(By.linkText("india")).click(); // To select a data from the drop down list.
Muhammed Refaat
  • 8,564
  • 12
  • 81
  • 114
Murali
  • 31
  • 5
1

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

Happy Coding :)

Abhishek Singh
  • 9,749
  • 20
  • 71
  • 102
0

I have to struggle to find how to achieve especial those who are new to this tool (like me)

C# code:

IWebElement ddl = ffDriver.FindElement(By.Id("ddlGoTo")); 
OpenQA.Selenium.Support.UI.SelectElement clickthis = new OpenQA.Selenium.Support.UI.SelectElement(ddl);
clickthis.SelectByText("Your Text");

hope this help others!

Nick Kahn
  • 19,034
  • 87
  • 266
  • 401
0
public static void mulptiTransfer(WebDriver driver, By dropdownID, String text, By to)
{   
    String valuetext = null;
    WebElement element = locateElement(driver, dropdownID, 10);
    Select select = new Select(element);
    List<WebElement> options = element.findElements(By.tagName("option"));
    for (WebElement value: options) 
    {
        valuetext = value.getText();
        if (valuetext.equalsIgnoreCase(text))
        {
            try
            {
                select.selectByVisibleText(valuetext);
                locateElement(driver, to, 5).click();                           
                break;
            }
            catch (Exception e)
            {
                System.out.println(valuetext + "Value not found in Dropdown to Select");
            }       
        }
    }
}
Helen
  • 71,797
  • 10
  • 199
  • 256
0
select = driver.FindElement(By.CssSelector("select[uniq id']"));
                selectElement = new SelectElement(select);
                var optionList =
                    driver.FindElements(By.CssSelector("select[uniq id']>option"));
                selectElement.SelectByText(optionList[GenerateRandomNumber(1, optionList.Count())].Text);
Emebet
  • 1
-2

You can use this

(new SelectElement(driver.FindElement(By.Id(""))).SelectByText("");
Mariusz Jamro
  • 29,116
  • 24
  • 107
  • 151
Madhu
  • 459
  • 4
  • 10