selenium - selenium python动态下拉菜单

80 1

我想自动从网站上填写表格,一切顺利,直到我需要从动态下拉菜单中选择一个选项。


<div id="field_type_group" class="field trigger-option group0" style="">


 <div class="label"><label>Input Type</label></div>


 <div class="input">


 <select>


 <option data-item-id="100" value="text_field_validation_type">Text input</option>


 <option data-item-id="200" value="dropdown_field_type">Dropdown list</option>


 <option data-item-id="300" value="location_field_type">Location based input</option>


 </select>


 </div>


</div>



<div id="dropdown_field_type" class="field trigger-option group200" style="">


 <div class="label"><label>Data source for dropdown list</label></div>


 <div class="input">


 <select>


 <option data-item-id="6" data-displayfield="dropdown_static" value="none">Populate with list items specified here</option>


 <option data-item-id="7" data-displayfield="dropdown_dynamic" value="none">Retrieve list items from my service</option>


 </select>


 </div>


</div>




wait.until(ec.visibility_of_element_located((By.XPATH,"//select[option[@value='text_field_validation_type']]"))).click()


browser.find_element_by_xpath(".//option[text()[contains(.,'Dropdown list')]]").click()



但是当我需要选择子菜单时出现问题。

我使用Select的代码:


select_element = browser.find_element_by_xpath("//select[option[@value='text_field_validation_type']]")


select = Select(select_element)


select.select_by_index(1)



select_element = browser.find_element_by_xpath("//select[option[@data-item-id='6']]")


select = Select(select_element)


select.select_by_index(1)



我的其他代码:


wait.until(ec.visibility_of_element_located((By.XPATH,"//select[option[@data-item-id ='6']]"))).click()


browser.find_element_by_xpath(".//option[@data-displayfield='dropdown_dynamic']").click()



时间: 原作者:

105 2

使用selenium Select基于标签的xpath,等待元素可以单击的代码,尝试下面的代码:


sub_menu = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@id='dropdown_field_type' and .//label[.='Data source for dropdown list']]//select")))


select = Select(sub_menu)


select.select_by_index(0)


# or


select.select_by_visible_text("Populate with list items specified here")



原作者:
99 5

尝试使用xpath?


dd1 ="//*[text() = 'Input Type']//following::select[1]"


dd2 ="//*[contains(text() , 'Data source')]//following::select[1]"



按文本或值选择第一个下拉列表。


select = Select(driver.find_element_by_xpath(dd1))



# select by visible text


select.select_by_visible_text('Dropdown list')


# OR select by value 


select.select_by_value('dropdown_field_type')



然后选择第二个下拉列表


select = Select(driver.find_element_by_xpath(dd2))


# select by visible text


select.select_by_visible_text('Populate with list items specified here')



请确保添加了正确的大小写的文本。

原作者:
...