繁体   English   中英

蟒蛇硒| 如何使用索引获取所有下拉列表值,复杂下拉结构

[英]Python selenium | How to fetch all the drop down list values using index , Complex drop down structure

我正在使用 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)

我得到空值作为输出:

<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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM