96

I am converting my selenium 1 code to selenium 2 and can't find any easy way to select a label in a drop down menu or get the selected value of a drop down. Do you know how to do that in Selenium 2?

Here are two statements that work in Selenium 1 but not in 2:

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");
Simulant
  • 19,190
  • 8
  • 63
  • 98
user786045
  • 2,498
  • 4
  • 22
  • 18
  • Have you tried to locate it using Firebug? Using the xpath generated with Firebug/xpather can make it easier. –  Jun 21 '11 at 19:11
  • 1
    The question is not about locating or finding the drop down. Its about selecting a label in that drop down. I can locate the drop down but don't know which method to call in Selenium 2 since select() and getSelectedValue() or getSelectedLabel() do not work in Selenium 2. – user786045 Jun 21 '11 at 19:16
  • Solution in c#: http://stackoverflow.com/questions/5278281/how-to-using-webdriver-selenium-for-selecting-an-option-in-c – steve cook Jul 27 '14 at 06:02

10 Answers10

184

Take a look at the section about filling in forms using webdriver in the selenium documentation and the javadoc for the Select class.

To select an option based on the label:

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

To get the first selected value:

WebElement option = select.getFirstSelectedOption()
matt freake
  • 4,877
  • 4
  • 27
  • 56
janderssn
  • 1,944
  • 1
  • 12
  • 3
  • By.xpath("//path_to_drop_down"). I would replace this with a locator like By.name as so on. – Daniel Apr 17 '12 at 02:09
  • 2
    deselectAll will throw an UnsupportedOperationException if the select does not support multiple selections – Tom Hartwell Mar 20 '13 at 19:42
  • 4
    In C#, use the SelectElement class, so: `SelectElement salesExecutiveDropDown = new SelectElement(webDriver.FindElement(By.Id("salesExecutiveId")));` – Jeremy McGee Jul 22 '13 at 14:21
  • Fyi this code was not able to select a dropdown until I commented out this line: //select.deselectAll(); Then it started working. Your mileage may vary. – HRVHackers May 01 '14 at 21:56
  • 1
    Note that `deselectAll` is only valid for multiselect: https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/Select.html#deselectAll(). – user1205577 Oct 21 '14 at 19:13
  • yes, I would delete line select.deselectAll(); as it doesn't work for most dropdowns, because those are single selection typically – kiedysktos Apr 07 '16 at 09:53
5
driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();
Halo
  • 1,730
  • 1
  • 8
  • 31
thrasher
  • 51
  • 1
  • 2
4

in ruby for constantly using, add follow:

module Selenium
  module WebDriver
    class Element
      def select(value)
        self.find_elements(:tag_name => "option").find do |option|
          if option.text == value
            option.click
              return
           end
       end
    end
  end
end

and you will be able to select value:

browser.find_element(:xpath, ".//xpath").select("Value")
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
AlekseiPetrovski
  • 1,016
  • 9
  • 17
3

Try using:

selenium.select("id=items","label=engineering")

or

selenium.select("id=items","index=3")
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
coolcub
  • 31
  • 1
0

A similar option to what was posted above by janderson would be so simply use the .GetAttribute method in selenium 2. Using this, you can grab any item that has a specific value or label that you are looking for. This can be used to determine if an element has a label, style, value, etc. A common way to do this is to loop through the items in the drop down until you find the one that you want and select it. In C#

int items = driver.FindElement(By.XPath("//path_to_drop_Down")).Count(); 
for(int i = 1; i <= items; i++)
{
    string value = driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).GetAttribute("Value1");
    if(value.Conatains("Label_I_am_Looking_for"))
    {
        driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).Click(); 
        //Clicked on the index of the that has your label / value
    }
}
Ben
  • 667
  • 6
  • 13
0

you can do like this :

public void selectDropDownValue(String ValueToSelect) 
{

    webelement findDropDownValue=driver.findElements(By.id("id1"))    //this will find that dropdown 

    wait.until(ExpectedConditions.visibilityOf(findDropDownValue));    // wait till that dropdown appear

    super.highlightElement(findDropDownValue);   // highlight that dropdown     

    new Select(findDropDownValue).selectByValue(ValueToSelect);    //select that option which u had passed as argument
}
t3dodson
  • 3,949
  • 2
  • 29
  • 40
Praveen
  • 378
  • 3
  • 6
0

This method will return the selected value for the drop down,

public static String getSelected_visibleText(WebDriver driver, String elementType, String value)
  {
    WebElement element = Webelement_Finder.webElement_Finder(driver, elementType, value);
   Select Selector = new Select(element);
    Selector.getFirstSelectedOption();
    String textval=Selector.getFirstSelectedOption().getText();
    return textval;
  }

Meanwhile

String textval=Selector.getFirstSelectedOption();

element.getText();

Will return all the elements in the drop down.

0

Select in Selenium WebDriver

The ‘Select’ class in Selenium WebDriver is used for selecting and deselecting the option in a dropdown. The objects of Select type can be initialized by passing the dropdown webElement as parameter to its constructor.

WebElement testDropDown = driver.findElement(By.id("testingDropdown")); Select dropdown = new Select(testDropDown);

Selecting options from dropdown

There are three ways of selecting options from dropdown-

  1. selectByIndex – To select an option based on its index, beginning with 0.

dropdown.selectByIndex(3);

  1. selectByValue – To select an option based on its ‘value’ attribute.

dropdown.selectByValue("Database");

  1. selectByVisibleText – To select an option based on the text over the option.

dropdown.selectByVisibleText("Database Testing");

Thom A
  • 88,727
  • 11
  • 45
  • 75
rohitss
  • 1
  • 1
0

Select a particular value in a dropdown using the methods of Select class in Selenium Select class implement select tag, providing helper methods to select and deselect options.

WebElement dropdownlist = driver.findElement(By.xpath(locator));
Select listbox = new Select(dropdownlist);

Use of methods of select class with examples:

selectByIndex(int index): Select the option at the given index.
listbox.selectByIndex(2);

selectByVisibleText(java.lang.String text): Select all options that display text matching the argument
listbox.selectByVisibleText(“Date”);

Please refer to the below link for more detailed discussions here

arpita biswas
  • 144
  • 1
  • 6
-2

This is the code to select value from the drop down

The value for selectlocator will be the xpath or name of dropdown box, and for optionLocator will have the value to be selected from the dropdown box.

public static boolean select(final String selectLocator,
        final String optionLocator) {
    try {
        element(selectLocator).clear();
        element(selectLocator).sendKeys(Keys.PAGE_UP);
        for (int k = 0; k <= new Select(element(selectLocator))
                .getOptions().size() - 1; k++) {
            combo1.add(element(selectLocator).getValue());
            element(selectLocator).sendKeys(Keys.ARROW_DOWN);
        }
        if (combo1.contains(optionLocator)) {
            element(selectLocator).clear();
            new Select(element(selectLocator)).selectByValue(optionLocator);
            combocheck = element(selectLocator).getValue();
            combo = "";

            return true;
        } else {
            element(selectLocator).clear();
            combo = "The Value " + optionLocator
                    + " Does Not Exist In The Combobox";
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        errorcontrol.add(e.getMessage());
        return false;
    }
}



private static RenderedWebElement element(final String locator) {
    try {

        return (RenderedWebElement) drivers.findElement(by(locator));
    } catch (Exception e) {
        errorcontrol.add(e.getMessage());
        return (RenderedWebElement) drivers.findElement(by(locator));
    }
}

Thanks,

Rekha.

Kartmcad
  • 25
  • 3