简体   繁体   中英

Selenium python trying to extract the first selected option value from the dropdown

I am trying to extract the first selected value from the dropdown. Not sure what I am wrong here

This is what I have tried so far:

currency = Select(self.driver.find_element_by_id("currencyid"))
currency.select_by_visible_text("AUD")

assert currency.first_selected_option().text == "AUD"

Here is the HTML code looks like:

在此处输入图片说明

This is the error I am getting, not sure how useful is this:

File "Test.py", line 54, in test_TestOne
    assert currency.first_selected_option().text == "USD"
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 62, in first_selected_option
    for opt in self.options:
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 47, in options
    return self._el.find_elements(By.TAG_NAME, 'option')
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 528, in find_elements
    {"using": by, "value": value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.25.426923 (),platform=Windows NT 10.0.10586 x86_64)
currency = Select(self.driver.find_element_by_id("currencyid"))
currency.select_by_visible_text("AUD")

assert currency.first_selected_option.text == "AUD"

You should use first_selected_option not first_selected_option()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM