2016-12-17 10 views
1

私はguideの後にPythonでTDDを学びます。 some pointでは、移行を行った後、コマンドpython3 functional_tests.pyの出力は(ブックによる)でなければなりません:セレンは死んだオブジェクトにアクセスできません/要素の参照が古くなっています

selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "tr" is invalid: TypeError: can't access dead object 

そして第二に(およびそれ以上)の時間を試した後:

self.fail('Finish the test!') 
AssertionError: Finish the test! 

しかし、私はエラーを取得しています:

selenium.common.exceptions.StaleElementReferenceException: Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed. 
私はグーグルで同様の問題を検索していますが、問題を解決するのに役立つものは見つかりませんでした。
私はgeckodriverを使用しており、そのパスを PATHに追加しています。

Django==1.8.7 
selenium==3.0.2 
Mozilla Firefox 50.0.2 
(X)Ubuntu 16.04 

Chromeに切り替える必要がありますか?それは自明ではない、それは私からいくつかの時間を必要とするだろうが、それは動作することができますか? Firefoxやセレンのようなものですか?私はそれがコード関連ではないと思う - 私はrepo for chapter 5をクローンし、同じクラッシュが起こっている。

+0

IMHO Chromeのサポートははるかに優れているようです。/usr/bin/chromeのようにchromiumとシンボリックリンクを設定するとクロムに変更することができます。 –

答えて

1

本書では、Selenium 3ではなく、Selenium 2を使用することを期待しているからです。v3では、暗黙の待ち時間(かなり前回チェックしたバグ)とはかなり異なる動作をしています。今。

は、インストール手順をもう一度見ている:以前の章で、我々はPOSTリクエストの後にリダイレクトを追加するためhttp://www.obeythetestinggoat.com/book/pre-requisite-installations.html

2

エラーが発生します。このページは簡単にリフレッシュされ、それがSeleniumを台無しにする可能性があります。 Selenium 3を使用したい場合は、書籍のブログ:http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.htmlでこれを修正しました。

基本的に、NewVisitorTestクラスにメソッドを追加すると、ページがリロードされるのを待ってから、アサートテストを続行できます。

... 
from contextlib import contextmanager 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support.expected_conditions import staleness_of 

class NewVisitorTest(unittest.TestCase): 
    ... 
    @contextmanager 
    def wait_for_page_load(self, timeout=30): 
     old_page = self.browser.find_element_by_tag_name("html") 
     yield WebDriverWait(self.browser, timeout).until(
      staleness_of(old_page) 
     ) 
    ... 
    def test_can_start_list_and_retrieve_it_later(self): 
     ... 
     inputbox.send_keys("Buy peacock feathers") 
     inputbox.send_keys(Keys.ENTER) 

     with self.wait_for_page_load(timeout=10): 
      self.check_for_row_in_list_table("1: Buy peacock feathers") 

     inputbox = self.browser.find_element_by_id("id_new_item") 
     inputbox.send_keys("Use peacock feathers to make a fly") 
     inputbox.send_keys(Keys.ENTER) 

     with self.wait_for_page_load(timeout=10): 
      self.check_for_row_in_list_table("1: Buy peacock feathers") 
      self.check_for_row_in_list_table("2: Use peacock feathers to make a fly") 
関連する問題