2011-10-23 78 views
68

我需要從下拉菜單中選擇一個元素。Selenium - Python - 下拉菜單選項值

例如,打開這樣的:那麼首先

<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> 
  1. 我必須點擊它。我這樣做:

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

(好吧,這打開菜單)

  • 後,我必須選擇好元素,可以說芒果。我嘗試與inputElementFruits.send_keys(...)不同的事情,但它沒有奏效。
  • 回答

    49

    除非您點擊發射某種ajax調用來填充您的列表,否則實際上並不需要執行點擊操作。

    只要找到元素,然後枚舉選項,選擇你想要的選項。

    下面是一個例子:

    from selenium import webdriver 
    b = webdriver.Firefox() 
    b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click() 
    

    你可以閱讀更多:
    https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver

    +7

    僅供參考,使用'Select'類使問題更容易解決,請參閱我發佈的答案。 – alecxe

    +0

    如果我正在使用'find_by_id',該怎麼辦? 那麼我該如何提供價值呢?另外,如何找到元素的'xpath'? –

    118

    硒提供了一個方便Select classselect -> option結構的工作:

    from selenium import webdriver 
    from selenium.webdriver.support.ui import Select 
    
    driver = webdriver.Firefox() 
    driver.get('url') 
    
    select = Select(driver.find_element_by_id('fruits01')) 
    
    # select by visible text 
    select.select_by_visible_text('Banana') 
    
    # select by value 
    select.select_by_value('1') 
    

    參見:

    +0

    這是一個很好的方法,應該是事實上的方法。不過,我會注意到,如果表單的作者未正確設置選定的HTML元素,則可能必須使用更鈍的「xpath」版本。如果簡單地使用輸入字段,xpath應該工作。 – Matthew

    0

    最好的方式來使用selenium.webdriver.support.ui.Select類的工作與下拉選擇,但一段時間,因爲預期是由於設計問題或HTML等問題這是行不通的。

    在這種類型的情況下,你也可以如下使用execute_script()喜歡作爲替代的解決方案: -

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

    我嘗試了很多很多東西,但我的下拉是表內的,我不能夠執行簡單的選擇操作。只有下面的解決方案工作。在這裏,我強調下拉ELEM並按向下箭頭,直到得到所需的值 -

     #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
    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) 
    

    它會正常工作

    2

    首先你需要導入選擇類,然後你需要創建Select類的實例。 創建Select類的實例後,可以在該實例上執行選擇方法以從下拉列表中選擇選項。 這裏是代碼

    from selenium.webdriver.support.select import Select 
    
    select_fr = Select(driver.find_element_by_id("fruits01") 
    select_fr.select_by_index(0)