How to select a drop-down menu option value with Selenium (Python)


I need to select an element from a drop-down menu.

For example:

<select id="fruits01" class="select" name="fruits">
  <option value="0">Choose your fruits:</option>
  <option value="1">Banana</option>
  <option value="2">Mango</option>

1) First I have to click on it. I do this:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

2) After that I have to select the good element, lets say Mango.

I tried to do it with inputElementFruits.send_keys(...) but it did not work.

All Answers
  • Translate

    Unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click.

    Just find the element and then enumerate the options, selecting the option(s) you want.

    Here is an example:

    from selenium import webdriver
    b = webdriver.Firefox()

    You can read more in:

  • Translate

    Selenium provides a convenient Select class to work with select -> option constructs:

    from selenium import webdriver
    from import Select
    driver = webdriver.Firefox()
    select = Select(driver.find_element_by_id('fruits01'))
    # select by visible text
    # select by value 

    See also:

  • Translate

    firstly you need to import the Select class and then you need to create the instance of Select class. After creating the instance of Select class, you can perform select methods on that instance to select the options from dropdown list. Here is the code

    from import Select
    select_fr = Select(driver.find_element_by_id("fruits01"))

  • Translate

    I tried a lot many things, but my drop down was inside a table and I was not able to perform a simple select operation. Only the below solution worked. Here I am highlighting drop down elem and pressing down arrow until getting the desired value -

            #identify the drop down element
            elem = browser.find_element_by_name(objectVal)
            for option in elem.find_elements_by_tag_name('option'):
                if option.text == value:
                    ARROW_DOWN = u'\ue015'

  • Translate

    You don't have to click anything. Use find by xpath or whatever you choose and then use send keys

    For your example: HTML:

    <select id="fruits01" class="select" name="fruits">
        <option value="0">Choose your fruits:</option>
        <option value="1">Banana</option>
        <option value="2">Mango</option>


    fruit_field = browser.find_element_by_xpath("//input[@name='fruits']")

    That's it.

  • Translate

    You can use a css selector combination a well

    driver.find_element_by_css_selector("#fruits01 [value='1']").click()

    Change the 1 in the attribute = value css selector to the value corresponding with the desired fruit.

  • Translate
    from import Select
    driver = webdriver.Ie(".\\IEDriverServer.exe")
    select = Select(driver.find_element_by_xpath("""//input[@name='n_name']"""))

    It will work fine

  • Translate

    The best way to use class to work to with dropdown selection but some time it does not work as expected due to designing issue or other issues of the HTML.

    In this type of situation you can also prefer as alternate solution using execute_script() as below :-

    option_visible_text = "Banana"
    select = driver.find_element_by_id("fruits01")
    #now use this to select option from dropdown by visible text 
    driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, option_visible_text);

  • Translate

    It works with option value:

    from selenium import webdriver
    b = webdriver.Firefox()

  • Translate
    1. List item

    public class ListBoxMultiple {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("", "./drivers/chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("file:///C:/Users/Amitabh/Desktop/hotel2.html");//open the website
        WebElement hotel = driver.findElement("maarya"));//get the element
        Select sel=new Select(hotel);//for handling list box
            System.out.println("it is multi select list");
            System.out.println("it is single select list");
        //select option
        sel.selectByIndex(1);// you can select by index values
        sel.selectByValue("p");//you can select by value
        sel.selectByVisibleText("Fish");// you can also select by visible text of the options
        //deselect option but this is possible only in case of multiple lists
        List<WebElement> options = sel.getOptions();
        int count=options.size();
        System.out.println("Total options: "+count);
        for(WebElement opt:options){ // getting text of every elements
            String text=opt.getText();
        //select all options
        for(int i=0;i<count;i++){