1

我目前在编写一种方法时遇到了一些困难,该方法允许我在 Select 元素中找到当前选定选项的值。

<select id="select_foo_bar">
   <option value="0">FOO</option>
   <option value="1" selected="selected">BAR</option>
   <option value ="2">FOOBAR</option>
</select>

目前我有这个:

def find_selected_option(self):
    self.wait.until(EC.presence_of_element_location((By.ID, "select_foo_bar"))
    option = Select(self.driver.find_element_by_id("select_foo_bar")).first_selected_option()
    return option.get_attribute("value")

据我了解,该方法将找到option元素,获取value并返回它。

不幸的是,我收到了错误TypeError: 'WebElement' object is not callable。这是option = Select(self.driver.find_element_by_id("select_foo_bar")).first_selected_option()在线上发生的。它甚至没有达到return声明的重点。

任何帮助将不胜感激。

4

2 回答 2

3

我已经解决了这个问题。这是因为我昨天用疲倦的眼睛阅读了文档。

需要调用的是first_selected_option和不是first_selected_option()

谢谢你们的帮助。

于 2013-06-04T14:07:36.560 回答
0

您是否在行尾缺少右括号option = Select(...

也许这就是你要找的东西:Selenium - Python - 下拉菜单选项值

self.driver.find_element_by_id("select_foo_bar")应该已经返回元素,你可以打电话self.driver.find_element_by_id("select_foo_bar").first_selected_option()吗?

于 2013-06-03T15:19:03.807 回答