2017-03-03 26 views
0

我想從下拉菜單中提取第一個選定的值。不知道我錯在這裏硒蟒蛇試圖從下拉菜單中提取第一個選定的選項值

這是我到目前爲止已經試過:

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

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

下面是HTML代碼如下所示:

enter image description here

這是我的錯誤得到,不知道這是多麼有用:

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) 
+1

'StaleElementReferenceException'意味着'selenium'擡頭你的'element'的'html'結構發生變化(有沒有在頁面上加載的東西你還是'select'被發現過嗎? ),然後您嘗試使用舊的「陳舊」元素。您最好的選擇可能是在您與下拉菜單進行互動之前等待指示頁面已加載的內容。爲了測試這個問題,在這個'Select(self.driver.find_element_by_id(「currencyid」))'之前進行幾秒鐘的睡眠,您可以計算出該如何工作的等待。 – mrfreester

+1

你想找到'selected'選項嗎? – Beomi

+0

@mrfreester,不知道發生了什麼事。我試圖增加5秒的睡眠時間,但它甚至不等待5秒鐘。它會立即給出這個錯誤 – user7242550

回答

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

assert currency.first_selected_option.text == "AUD" 

您應該使用first_selected_optionfirst_selected_option()

+0

我已經試過了。但不起作用 – user7242550

+0

這意味着,你有完全相同的錯誤? – Beomi

+0

,我在代碼中找不到'currency.first_selected_option()。text ==「USD」''。 – Beomi

相關問題