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

问题描述 投票:-1回答:3

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

我想从我的代码中的下拉列表中获取所有可用值,以便我将执行一些逻辑函数来发送下一个下拉值的值,请帮我这个

python python-2.7 selenium selenium-webdriver dropdown
3个回答
0
投票

处理<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

0
投票

您可以尝试使用此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)  

希望这可以帮助。


-1
投票

要获取所有选项的文本,请使用以下代码:

listele = driver.find_element_by_xpath("//*[@name='evo_lead_evo_finance_product_id']/option")

for i  in range(len(listele)):
    print(listele[i].text)
© www.soinside.com 2019 - 2024. All rights reserved.