selenium select class get return option value empty string

时间:2017-04-08 16:20:23

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

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

但是,当我只是选择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 [&lt;   selenium.webdriver.remote.webelement.WebElement   (session =&#34; 54c42ae6-e18a-40bb-b497-f2a6b56bc98e&#34;,element =&#34; 12&#34;)&gt; ,&lt;   selenium.webdriver.remote.webelement.WebElement   (session =&#34; 54c42ae6-e18a-40bb-b497-f2a6b56bc98e&#34;,element =&#34; 13&#34;)&gt; ,&lt;   selenium.webdriver.remote.webelement.WebElement   (session =&#34; 54c42ae6-e18a-40bb-b497-f2a6b56bc98e&#34;,element =&#34; 14&#34;)&gt; ,&lt;   selenium.webdriver.remote.webelement.WebElement   (session =&#34; 54c42ae6-e18a-40bb-b497-f2a6b56bc98e&#34;,element =&#34; 15&#34;)&gt; ]   {STR}&#39;&#39; 5

1 个答案:

答案 0 :(得分:0)

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

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