Selenium-如何获取“选择”字段的值选项-PYTHON

时间:2019-02-27 17:33:53

标签: python-3.x selenium

我有以下关注选择字段:

<select size="4" name="ctl00$_contentPlaceHolderOiLegal$lstRequisicoes" id="ctl00__contentPlaceHolderOiLegal_lstRequisicoes" tabindex="67">
   <option value="103122 - Registration data [1]">103122 - Registration data [1]</option>
   <option selected="selected" value="103123 - Sittel - Extrato com cadastro de interlocutor [2]">103123 - Sittel - Extract with contact record [2]</option>
</select>

如何获取字段的值并将其转换为数组?

我尝试了,但不起作用:

   name = driver.find_element_by_id("ctl00__contentPlaceHolderOiLegal_lstRequisicoes").get_attribute("value")

    print(name)

2 个答案:

答案 0 :(得分:2)

要获取所选dropdwon的值,请尝试此操作。您需要导入select,让我知道您正在照顾。

from selenium.webdriver.support.ui import Select

select=Select(driver.find_element_by_id("ctl00__contentPlaceHolderOiLegal_lstRequisicoes"))
seltext = select.first_selected_option
print(seltext.text)

输出:

103123 - Sittel - Extract with contact record [2]

from selenium.webdriver.support.ui import Select    select=Select(driver.find_element_by_id("ctl00__contentPlaceHolderOiLegal_lstRequisicoes"))
    seltext = select.first_selected_option
    print(seltext.get_attribute('value'))

输出:

103123 - Sittel - Extrato com cadastro de interlocutor [2]

答案 1 :(得分:1)

使用以下代码:

    listOfOptions = driver.find_elements_by_xpath('//select[@id='ctl00__contentPlaceHolderOiLegal_lstRequisicoes']//option')

option_value_array[]

    for option in listOfOptions:

        option_value = option.text
        option_value_array.append(option_value)
        print (option_value)