2017-07-24 34 views
1

我有一個下拉菜單,下面的html代碼:我需要在下拉菜單項聚集成一個Python列表

<div class="dropdown open"> 
    <button aria-expanded="true" class="btn btn-default btn-block dropdown-toggle" type="button" id="menu3" data-toggle="dropdown"> 
     <span class="btn-chevron"></span> 
     <span class="btn-label" id="assign_product_selection">Select Product</span> 
    </button> 
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu3">     
     <li role="presentation"><a role="menuitem" onclick="select_assignreadingproduct(&quot;Product One&quot;, 76997)">Product One</a></li>     
     <li role="presentation"><a role="menuitem" onclick="select_assignreadingproduct(&quot;Product Three&quot;, 76997)">Product Three</a></li>  
     <li role="presentation"><a role="menuitem" onclick="select_assignreadingproduct(&quot;Product Two&quot;, 76997)">Product Two</a></li>  
    </ul> 
</div> 

我試圖讓在下降中的所有項目的值下拉菜單:即(件產品,產品二,產品分類三),並把它們放入一個Python列表。這是我目前的,但它只是返回一個空的產品名稱列表。

product_menu = driver.find_element_by_id('menu3') 
    product_options = product_menu.find_elements_by_xpath("//*[@role = 'menuitem']") 
    list_of_product_names = [] 
    if len(product_options) < 1: 
     print 'no products listed' 
    else: 
     for item in product_options: 
      val_item = item.text 
      list_of_product_names.append(val_item) 
    print 'list of product names',list_of_product_names 

我的輸出:

產品名稱[U '',U '',U '',U '',U '',U '',U '',U名單'',U '', U '' U '',U '',U '',U '',U '',U '',U '',U '',U '',U」 」 U '' U '',U '', U '' U '',U '',U '',U '']

+0

因爲這不是一個真正的下拉列表中,'SELECT'元素,你有沒有嘗試過點擊下拉菜單,以便'OPTIONS'是可見的? – JeffC

+0

工作表示感謝! –

+0

太棒了。我將我的評論轉到了答案。請給予好評,如果你發現它有用(以及任何其他的答案是有用的),並把它標記爲接受這樣的問題被標記爲回答。 – JeffC

回答

1

我的評論移動到一個答案,因爲它解決了問題。

因爲這不是一個真正的下拉菜單中選擇元素,單擊下拉,這樣的選項是可見的。