Joshua Clew
Joshua Clew

Reputation: 80

I need to gather the drop down menu items into a python list

I have a drop down menu with the following html code:

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

I am trying to get the values of all of the items in the drop down menu: i.e. (Product One, Product Two, Product Three) and put them into a python list. Here is what I have currently but it just returns a list of product names that are empty.

    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

My Output :

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

Upvotes: 1

Views: 279

Answers (1)

JeffC
JeffC

Reputation: 25611

Moving my comment to an answer since it solved the problem.

Since this is not a true dropdown, SELECT element, click the dropdown so that the OPTIONS are visible.

Upvotes: 1

Related Questions