簡體   English   中英

Selenium python嘗試從下拉列表中提取第一個選定的選項值

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

我正在嘗試從下拉列表中提取第一個選定的值。 不知道我在這里錯了

到目前為止,這是我嘗試過的:

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

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

HTML代碼如下所示:

在此處輸入圖片說明

這是我得到的錯誤,不確定這有多有用:

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"

您應該使用first_selected_option而不是first_selected_option()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM