2017-03-03 12 views
0

ドロップダウンから最初に選択した値を抽出しようとしています。ここでSelenium pythonがドロップダウンから最初に選択したオプション値を抽出しようとしています

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'を見上げたことを意味使用する必要がありますか? )、古い '古い'要素を使用しようとしました。おそらく、ドロップダウンと対話する前に、ページがロードされていることを示す何かを待つことが最善の策でしょう。これをテストするには問題があります。この 'Select(self.driver.find_element_by_id(" currencyid "))'の前に数秒間スリープ状態にしておけば、それが動作するかどうかを調べることができます。 – mrfreester

+1

'selected'オプションを探したいですか? – Beomi

+0

@mrfreester、何が起こっているのか分かりません。私は5秒の睡眠を追加しようとしましたが、5秒間待つことさえありません。このエラーはすぐに – user7242550

答えて

0

あなたは何かのロードがまだあなたの `SELECT`が発見された前のページにあり、` html`構造は(変更first_selected_optionがないfirst_selected_option()

+0

私は両方を試みました。しかし、動作しません – user7242550

+0

それは、あなたはまったく同じエラーを持っていることを意味ですか? – Beomi

+0

とあなたのコードに 'assert currency.first_selected_option()。text ==" USD "'が見つかりませんでした。 – Beomi

関連する問題