【问题标题】:selenium Select class get return option value empty stringselenium Select 类获取返回选项值空字符串
【发布时间】:2017-09-19 15:42:36
【问题描述】:

我想使用带有 selenium 的 python 从多选框中进行选择。从我的代码中,我可以打开下拉列表或从框中找到选项的长度。

但是当我只是通过select_by_indexselect_by_visible_textselect_by_value 选择选项时,我总是收到错误Message: element not visible: Element is not currently visible and may not be manipulated 当我只是从选项中打印文本时,我总是得到空字符串 {str}''

多选看起来像来自链接https://jedwatson.github.io/react-select/的多选部分

我的代码

e = Select(driver.find_element_by_name('selectName'))
print(e)
print(e.options)
for o in e.options:
    print(o)
    print(o.text)

print(len(e.options))
e.select_by_index(1)
e.select_by_visible_text("A")
e.select_by_value("A")
print(e.all_selected_options)
time.sleep(10)
driver.close()

HTML 格式:

<div class="four wide column">
    <div id="name" role="combobox" aria-busy="false" aria-disabled="false" aria-expanded="false" class="ui fluid multiple search selection scrolling dropdown">
    <select type="hidden" aria-hidden="true" name="selectName" multiple="">
        <option value=""></option>
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
        <option value="D">D</option>
    </select>
    <a class="ui label" value="A"><!-- react-text: 4884 -->A<!-- /react-text --><i aria-hidden="true" class="delete icon"></i></a>
    <input type="text" value="" aria-autocomplete="list" class="search" name="Name-search" autocomplete="off" tabindex="0">
    <div class="text"></div>
    <i aria-hidden="true" class="dropdown icon"></i>
    <div aria-multiselectable="true">
        <div role="option" aria-checked="false" aria-selected="true" class="selected item"><!-- react-text: 1386 -->A<!-- /react-text --></div>
        <div role="option" aria-checked="false" aria-selected="false" class="item"><!-- react-text: 5240 -->B<!-- /react-text --></div>
    </div>
</div>

打印结果:

select.options [ , , , ] {str}'' 5

【问题讨论】:

  • 检查您提供的代码——您的 xpath 选择器无效(错字)?它会选择 div,而不是选择元素,这也是错误的。

标签: python python-3.x selenium selenium-webdriver drop-down-menu


【解决方案1】:

如下更改选择行中的 xpath。在您的 xpath 中,您使用的是与 div 元素相关联的 id='selectName' 而不是选择元素。

select = Select(driver.find_element_by_xpath("//*[@name='selectName']"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-22
    • 2010-10-20
    • 2017-06-14
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    相关资源
    最近更新 更多