Selenium Webdriver Python如何从禁用的下拉列表中获取所选值

时间:2015-10-06 11:25:10

标签: python-2.7 selenium selenium-webdriver

我遇到过残疾人下降。我试图从下拉列表中获取所选值。我想知道在下拉列表中选择了什么值 运行我的代码时出现以下错误:

datamap_dropdown_value = datamapdropdown_value.first_selected_option()
TypeError: 'WebElement' object is not callable

方法实现是:

def get_datamap_dropdown_field_value(self):
    datamapdropdown_value = Select(self.driver.find_element(By.ID, 'match_configuration_edit_match_lb_datamaps))
    datamap_dropdown_value = datamapdropdown_value.first_selected_option()
    print datamap_dropdown_value
    return datamap_dropdown_value

从这里调用该方法:

# Check name, datamap dropdown fields from details tab have saved correctly
def verify_matches_details_tab_fields_have_saved(self, name):
    name_field_value = self.get_name_field_value(name)
    datamap_dropdown_value = self.get_datamap_dropdown_field_value()
    return (name_field_value == name) and (datamap_dropdown_value == self.datamap_name)

HTML是:

<div class="clear">
    <span class="gwt-InlineLabel marginbelow myinlineblock" style="width: 8em;">Datamap</span>
        <select id="match_configuration_edit_match_lb_datamaps" class="gwt-ListBox marginbelow" style="display: inline;" disabled="">
            <option value="ceb09_16_1512_26_23">ceb09_16_1512_26_23</option>
        </select>
</div>

谢谢, 里亚兹

1 个答案:

答案 0 :(得分:1)

您可以通过调用driver.find_element_by_id("match_configuration_edit_match_lb_datamaps").text

等文本来获取元素的值
>>> elm = driver.find_element_by_id("match_configuration_edit_match_lb_datamaps")
>>> print elm.text
ceb09_16_1512_26_23