如何在下拉菜单中查看文本选项 [英] How to view text options within a dropdown menu

查看:53
本文介绍了如何在下拉菜单中查看文本选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择列表的HTML如下:

The HTML of the select list looks like:

<select name="date_range_month_start" id="date_range_month_start" data-width="109px" data-search="true" style="width: 109px; display: none;">
                    <option value="0">January</option>
                    <option value="9">October</option>
                    <option value="10" selected="selected">November</option>
                    <option value="11">December</option>
                 </select>

<div class="chosen-container chosen-container-single chosen-container-single-nosearch" style="width: 109px;" title="" id="date_range_month_start_chosen">
        <a class="chosen-single" tabindex="-1">
            <span>January</span>
                    <div><b></b>
                    </div>
                  </a>
    <div class="chosen-drop">...</div>
    </div>

但是,当我跑步时:

month = driver.find_element_by_id('date_range_month_start_chosen')
month.click() ## make dropdown list visible in browser
mySelect = Select(driver.find_element_by_css_selector("#date_range_month_start"))
print([o.text for o in mySelect.options])

它打印: ['','','','','','','','','','','',''] 我也尝试了其他一些方法,但是到目前为止,在此下拉菜单中打印文本值完全失败.

It prints: ['', '', '', '', '', '', '', '', '', '', '', ''] I've tried a few other things as well but so far have been entirely unsuccessful at printing what the text values in this drop down menu.

推荐答案

这里有几点:

  1. 使用 Select 类与 select options 标签一起使用.
  2. 我们应该仅通过 Select 类型的对象来尝试选择选项.
  3. 以下是代码块供您参考:

  1. Use the Select Class to work with the select and options tags.
  2. We should try to select an option only through Select type of object.
  3. Here is the code block for your reference :

selectmonth = Select(driver.find_element_by_id('date_range_month_start'))
for option in selectmonth.options:
    print(option.text)      

更新

如果由于 style ="width:109px; display:none; 而遇到 ElementNotVisibleException ,请使用以下代码块:

Update

If you face a ElementNotVisibleException due to style="width: 109px; display: none; use this block of code:

element = driver.find_element_by_id('date_range_month_start')   
driver.execute_script("return arguments[0].removeAttribute('style');", element)
selectmonth = Select(driver.find_element_by_id('date_range_month_start'))
for option in selectmonth.options:
    print(option.text)      

这篇关于如何在下拉菜单中查看文本选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆