0

我想使用python与硒从多选框中选择。从我的代码中,我可以打开下拉列表或从框中找到选项的len。硒选择类获取返回选项值空字符串

但是,当我刚通过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 [< selenium.webdriver.remote.webelement.WebElement (会话= “54c42ae6-E18A-40BB-b497-f2a6b56bc98e”,元素= “12”)>,< selenium.webdriver.remote.webelement.WebElement (会话= “54c42ae6-E18A-40BB-b497-f2a6b56bc98e”,元素= “13”)>,< selenium.webdriver.remote.webelement .WebElement (session =“54c42ae6-e18a-40bb-b497-f2a6b56 bc98e”,元素= “14”)>,< selenium.webdriver.remote.webelement.WebElement (会话= “54c42ae6-E18A-40BB-b497-f2a6b56bc98e”,元素= “15”)>] {STR} ''5

+0

检查您提供的代码 - 您的xpath选择器无效(打印错误)?它会选择div,而不是select元素,这也是错误的。 – pbuck

回答

0

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

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