1

I'm currently experiencing some difficulties writing a method that will allow me to find the value of the currently selected option within a Select element.

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

Currently I have this:

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

As I understand it, that method will find the option element, get the value and return it.

Unfortunately, I'm receiving the error TypeError: 'WebElement' object is not callable. This is occurring on the option = Select(self.driver.find_element_by_id("select_foo_bar")).first_selected_option() line. It doesn't even reach the point of the return statement.

Any help would be gratefully received.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
  • 1
    Have you tried debugging by breaking down the line starting `option = `, so you first find the ` – Vince Bowdren Jun 03 '13 at 16:02
  • Hi @vincebowdren, I probably should have mentioned before that I have stepped through, broken it down into local variables a number of times. Whilst stepping through, it successfully reaches the .first_selected_option() method from the Select class. It does proceed through the loop and returns the second option, as I would expect. Yet it is after that point that I receive the error detailed above. – Mark Rowlands Jun 04 '13 at 08:10
  • When you've first found the ` – Vince Bowdren Jun 04 '13 at 08:51
  • It finds the element fine, casts it fine and loops through the options to find the 'selected' option. At that point it appears to return that `WebElement` but it blows up after its found it. – Mark Rowlands Jun 04 '13 at 11:02

2 Answers2

3

I have solved this issue. It was due to me reading the docs with tired eyes yesterday.

The call is needed was first_selected_option and not first_selected_option()

Thanks for the help guys.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
0

Are you missing a closing parenthesis at the end of the option = Select(... line?

Perhaps this is what you are looking for: Selenium - Python - drop-down menu option value

self.driver.find_element_by_id("select_foo_bar") should already return the element, can you just call self.driver.find_element_by_id("select_foo_bar").first_selected_option()?

Community
  • 1
  • 1