我正在使用python selenium,我想从主屏幕中显示的下拉列表中获取所有值,下拉值是基于另一个下拉列表选择的动态
我试过以下代码
try:
element = find_element_by_locator(self, locator_type, locator)
print " -- Drop Down available values :"
for option in element.find_elements_by_tag_name('option'):
print " -- ", option.text
if option.text in select_text_option:
option.click()
except TimeoutException:
ex_message = " ** failed to get drop down value for " , select_text_option
print ex_message
raise Exception(ex_message)
我得到null值作为输出:
<select chosendataplaceholder="Choose Finance Product" class="chosen-select-width" error_target_sel="#evo_lead_evo_finance_product_id_err" name="evo_lead_evo_finance_product_id" data-placeholder="Choose Finance Product" style="display: none;">
<option selected="" value="1">Novated Finance Lease - Allowed</option>
<option value="2">Finance Lease - Not Allowed</option>
<option value="3">Novated Operating Lease - Not Allowed</option>
<option value="7">Chattel Mortgage - Not Allowed</option>
<option value="9">Consumer Loan - Not Allowed</option>
<option value="10">Associate Lease - Not Allowed</option>
<option value="11">No Finance (Car Only) - Not Allowed</option>
</select>
我想从我的代码中的下拉列表中获取所有可用值,以便我将执行一些逻辑函数来发送下一个下拉值的值,请帮我这个
处理<select>
下拉列表的正确方法是使用Select
类
element = find_element_by_name('evo_lead_evo_finance_product_id')
select = Select(element)
# select an option by text
select.select_by_visible_text(select_text_option)
# get all options text
for option in select.options:
print " -- ", option.text
您可以尝试使用此xpath,而不是标记名称,尽管它在我的系统中正常工作:
dropdown_elements = driver.find_elements_by_xpath("//select[@name='evo_lead_evo_finance_product_id']/option")
for element in dropdown_elements:
print(element.text)
希望这可以帮助。
要获取所有选项的文本,请使用以下代码:
listele = driver.find_element_by_xpath("//*[@name='evo_lead_evo_finance_product_id']/option")
for i in range(len(listele)):
print(listele[i].text)