How to select a drop-down menu option value using Selenium - Python

22,155

Solution 1

From the official documentation:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('fruitType'))
# Now we have many different alternatives to select an option.
select.select_by_index(4)
select.select_by_visible_text("jumbo fruit 4")
select.select_by_value('4') #Pass value as string

Solution 2

you can iterate trough all options like this:

element = driver.find_element_by_xpath("//select[@name='fruitType']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
    print("Value is: %s" % option.get_attribute("value"))
    option.click()

Solution 3

Hi please simply use one line code it will work

// please note the if in case you have to select a value form a drop down with tag 
// name Select then use below code it will work like charm
driver.find_element_by_id("fruitType").send_keys("jumbo fruit 4");

Hope it helps

Share:
22,155
404
Author by

404

Updated on July 09, 2022

Comments

  • 404
    404 almost 2 years

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

    <select class="chosen" id="fruitType" name="fruitType">
        <option value="">Select</option>
        <option value="1">jumbo fruit 1</option>
        <option value="2">jumbo fruit 2</option>
        <option value="3">jumbo fruit 3</option>
        <option value="4">jumbo fruit 4</option>
        <option value="5">jumbo fruit 5</option>
        <option value="8">jumbo fruit 6</option>
    </select>
    

    I have tried using this code,

    driver = webdriver.Firefox()
    driver.find_element_by_xpath("//select[@name='fruitType']/option[text()='jumbo fruit 4']").click()
    

    but it returned me with errors. How can I accomplish the same.

  • 404
    404 about 8 years
    For this answer i am getting the following error driver.find_element_by_id("fruitType").sendKeys("jumbo fruit 4"); AttributeError: 'WebElement' object has no attribute 'sendKeys'
  • eduliant
    eduliant about 8 years
    Sorry for python please use send_keys ,i am form java so it was a typo error also i have updated sendkeys for python
  • 404
    404 about 8 years
    so is it like select = Select(driver.find_element_by_xpath("//select[@id='fruitType‌​' and @class='chosen']")) driver.find_element_by_id("fruitType").send_keys("jumbo fruit 4); ??
  • eduliant
    eduliant about 8 years
    no no select statement not this line select = Select(driver.find_element_by_xpath("//select[@id='fruitType‌​' and @class='chosen']")) .only this line driver.find_element_by_id("fruitType").send_keys("jumbo fruit 4);
  • eduliant
    eduliant about 8 years
    Please use a universal wait for driver as error shows ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with. apply something like wait of some seconds before option selection
  • 404
    404 about 8 years
    will time.sleep(5) do ?
  • eduliant
    eduliant about 8 years
    driver.implicitly_wait(10) # seconds or time.sleep(NUM)