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

前端 未结 13 1885
南方客
南方客 2020-11-22 06:33

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

For example:


                        
    
提交评论

  • 2020-11-22 07:01

    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:
                    break
    
                else:
                    ARROW_DOWN = u'\ue015'
                    elem.send_keys(ARROW_DOWN)
    
    0 讨论(0)
  • 2020-11-22 07:02

    I hope this code will help you.

    from selenium.webdriver.support.ui import Select
    

    dropdown element with id

    ddelement= Select(driver.find_element_by_id('id_of_element'))
    

    dropdown element with xpath

    ddelement= Select(driver.find_element_by_xpath('xpath_of_element'))
    

    dropdown element with css selector

    ddelement= Select(driver.find_element_by_css_selector('css_selector_of_element'))
    

    Selecting 'Banana' from a dropdown

    1. Using the index of dropdown

    ddelement.select_by_index(1)

    1. Using the value of dropdown

    ddelement.select_by_value('1')

    1. You can use match the text which is displayed in the drop down.

    ddelement.select_by_visible_text('Banana')

    0 讨论(0)
  • 2020-11-22 07:03
    from selenium.webdriver.support.ui import Select
    driver = webdriver.Ie(".\\IEDriverServer.exe")
    driver.get("https://test.com")
    select = Select(driver.find_element_by_xpath("""//input[@name='n_name']"""))
    select.select_by_index(2)
    

    It will work fine

    0 讨论(0)
  • 2020-11-22 07:06

    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()
    b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
    

    You can read more in:
    https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver

    0 讨论(0)
  • 2020-11-22 07:08

    As per the HTML provided:

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

    To select an <option> element from a html-select menu you have to use the Select Class. Moreover, as you have to interact with the drop-down-menu you have to induce WebDriverWait for the element_to_be_clickable().

    To select the <option> with text as Mango from the dropdown you can use you can use either of the following Locator Strategies:

    • Using ID attribute and select_by_visible_text() method:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.support.ui import Select
      
      select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "fruits01"))))
      select.select_by_visible_text("Mango")
      
    • Using CSS-SELECTOR and select_by_value() method:

      select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.select[name='fruits']"))))
      select.select_by_value("2")
      
    • Using XPATH and select_by_index() method:

      select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//select[@class='select' and @name='fruits']"))))
      select.select_by_index(2)
      
    0 讨论(0)
  • 提交回复
    热议问题