Search code examples
pythonselenium-webdriverdrop-down-menuverification

Selenium: How can I verify the currently selected option in a dropdown?


I have a dropdown menu with several objects that can be chosen: Dropdown

I want to verify the default select option of 2 hours. I have done this so far with the following:

   def time_span_default(self):
    dropdown = Select(self.driver.find_element(*Elements.timespan_dropdown))
    default_option = dropdown.first_selected_option
    return default_option.text

 # If default_option.text == "2 hours" my testcase is verified and can continue...

However I also want to verify the text for the other objects once they are selected by me.

Can anyone advise what the best way to approach this would be if I want to verify the text of the currently selected object ?


Solution

  • first_selected_option should do the job even when you change the selected option. From the docs

    The first selected option in this select tag (or the currently selected option in a normal select)