title: getting current <select> value from drop-down menu with Python Selenium
body: <p>I am checking the selected value from a drop down field on a web page in Selenium Python.  I would like to print out what the selected value is.  I am getting all of the values from the drop down printed out.
  E.g. The drop down has a list of the following values: "Floating Point", "Date/Time", "Text string", "Integer"
  E.g. The selected value is "Text String"
  When i print the value it is printing out "Floating pointDate/timeText stringInteger"</p>
  
  <p>My code snippet is:</p>
  
  <pre><code>def get_selected_value_from_user_defined_type_dropdown(self, type):
      # Params : the selected value for the user defined type dropdown e.g. Text string
      user_defined_type_dropdown_element = self.get_element(By.XPATH, '//table[@id="data_configuration_edit_data_object_tab_details_tb_fields"]/tbody/tr[1]//td[3]//select')
      print ("------------------------------")
      print user_defined_type_dropdown_element.text
      return user_defined_type_dropdown_element.text
  </code></pre>
  
  <p>Get Element is in my base class, the implmentation is:</p>
  
  <pre><code># returns the element if found
  def get_element(self, how, what):
      # params how: By locator type
      # params what: locator value
      try:
          element = self.driver.find_element(by=how, value=what)
      except NoSuchElementException, e:
          print what
          print "Element not found "
          print e
          screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time.  This way the screenshot name will be unique and be able to save
          self.save_screenshot(screenshot_name)
          raise
      return element
  </code></pre>
  
  <p>The HTML is:</p>
  
  <pre><code>&lt;table id="data_configuration_edit_data_object_tab_details_tb_fields" class="GJPPK2LBJE border" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true"&gt;
  &lt;thead aria-hidden="false"&gt;
  &lt;colgroup&gt;
  &lt;tbody style=""&gt;
      &lt;tr class="GJPPK2LBCD GJPPK2LBJD" __gwt_subrow="0" __gwt_row="0"&gt;
      &lt;td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBED GJPPK2LBKD"&gt;
      &lt;td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD"&gt;
      &lt;td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD"&gt;
          &lt;div __gwt_cell="cell-gwt-uid-214" style="outline-style:none;"&gt;
              &lt;select tabindex="-1"&gt;
                  &lt;option value="Floating point"&gt;Floating point&lt;/option&gt;
                  &lt;option value="Date/time"&gt;Date/time&lt;/option&gt;
                  &lt;option selected="selected" value="Text string"&gt;Text string&lt;/option&gt;
                  &lt;option value="Integer"&gt;Integer&lt;/option&gt;
          &lt;/select&gt;
          &lt;/div&gt;
      &lt;/td&gt;
          &lt;td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBOD GJPPK2LBKD"&gt;
      &lt;/tr&gt;
      &lt;tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="1"&gt;
  &lt;/tbody&gt;
  &lt;tbody style="display: none;"&gt;
  &lt;tfoot style="display: none;" aria-hidden="true"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>How do i print out what the selected value is?</p>
  
  <p>I have tried to use first_selected_option but i get the following error:</p>
  
  <pre><code>    Traceback (most recent call last):
    File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\TestCases\DataObjectsPage_TestCase.py", line 294, in testk_edit_Data_Objects_ACVSEQ_Is_The_Saved_Details_Present
      self.assertTrue(data_objects_edit_page.is_value_saved_from_user_defined_type_dropdown("Text string"), "Data Objects ACVSEQ type drop down does not show the expected saved value. Please see log for details")
    File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\Pages\data_objects_edit.py", line 140, in is_value_saved_from_user_defined_type_dropdown
      return self.get_selected_value_from_user_defined_type_dropdown(str(value)) == value
    File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\Pages\data_objects_edit.py", line 133, in get_selected_value_from_user_defined_type_dropdown
      selected_option_element = user_defined_type_dropdown_element.first_selected_option
  AttributeError: 'WebElement' object has no attribute 'first_selected_option'
  </code></pre>
  
  <p>My code snippet is:</p>
  
  <pre><code>def get_selected_value_from_user_defined_type_dropdown(self, type):
      #Params : the selected value for the user defined type dropdown e.g. Text string
      user_defined_type_dropdown_element = self.get_element(By.XPATH, '//table[@id="data_configuration_edit_data_object_tab_details_tb_fields"]/tbody/tr[1]//td[3]//select')
      selected_option_element = user_defined_type_dropdown_element.first_selected_option
      print ("------------------------------")
      print selected_option_element.text
      return selected_option_element.text
  </code></pre>
  
  <p>I got the snippet for Python to use first_selected_option from this post:
  <a href="https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver">https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver</a></p>
  
  <p>Why is first_selected_option not working?  What is the syntax please?</p>
  
  <p>Thanks,
  Riaz</p>