我需要将下拉菜单项收集到python列表中

时间:2017-07-24 18:04:08

标签: python python-2.7 selenium selenium-webdriver

我有一个下拉菜单,其中包含以下HTML代码:

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

我正在尝试获取下拉菜单中所有项目的值:ie(Product One,Product Two,Product Three)并将它们放入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'',   你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你',你',   你,你,你,你,你,你,你,

1 个答案:

答案 0 :(得分:1)

将评论移至答案,因为它解决了问题。

由于这不是真正的下拉列表,SELECT元素,请单击下拉列表以使OPTIONS可见。