间歇性StaleElementReferenceException

时间:2018-09-01 11:58:09

标签: python tdd

研究Python的TDD,第5章,Django 1.8,Python 2.7,以及引入CSRF令牌后,事情变得很奇怪。我已经阅读了许多其他文章,并且能够解决大多数问题,但是我在解决这一问题时遇到了麻烦...

我不断收到StaleReferenceException,但无法复制。它间歇性地出现。如果我连续两次运行functional_test.py,则测试最终将按预期返回...

我不确定这是时间问题还是其他问题。关于如何进行追踪的任何想法?

(venv_unittesting) roadblock:superlists Admin$ python functional_test.py 
E
======================================================================
ERROR: test_open_home_page_header_input_table (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "functional_test.py", line 39, in test_open_home_page_header_input_table
    rows = table.find_elements_by_tag_name('tr')
  File "/Users/Admin/coding/django/venv_unittesting/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 320, in find_elements_by_tag_name
    return self.find_elements(by=By.TAG_NAME, value=name)
  File "/Users/Admin/coding/django/venv_unittesting/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 680, in find_elements
    {"using": by, "value": value})['value']
  File "/Users/Admin/coding/django/venv_unittesting/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/Users/Admin/coding/django/venv_unittesting/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "/Users/Admin/coding/django/venv_unittesting/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: The element reference of <table id="id_list_table"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed


----------------------------------------------------------------------
Ran 1 test in 9.359s

FAILED (errors=1)
(venv_unittesting) roadblock:superlists Admin$ python functional_test.py 
F
======================================================================
FAIL: test_open_home_page_header_input_table (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "functional_test.py", line 43, in test_open_home_page_header_input_table
    table.text,)
AssertionError: Not located on this page:
Buy Peacock Feathers

----------------------------------------------------------------------
Ran 1 test in 8.907s

FAILED (failures=1)
(venv_unittesting) roadblock:superlists Admin$ 

Functional_test.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

import time


class NewVisitorTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(2)

    def tearDown(self):
        self.browser.quit()
        # self.fail('Finish Testing! or put next task here')



    #A new user heard about a Todo list and want to to
    # Open the add

    def test_open_home_page_header_input_table(self):
        self.browser.get('http://localhost:8000')

        self.assertIn('To-Do', self.browser.title)
        header_text = self.browser.find_element_by_tag_name('h1').text
        self.assertIn('To-Do', header_text)
        inputbox = self.browser.find_element_by_id('id_new_item')
        self.assertEqual(inputbox.get_attribute('placeholder'),
                        'Enter a to-do item')

        inputbox.send_keys('Buy peacock Feathers')
        inputbox.send_keys(Keys.ENTER)

        # time.sleep(10)

        table = self.browser.find_element_by_id('id_list_table')
        rows = table.find_element_by_tag_name('tr')
        # self.assertTrue(
        #   any(row == '1: Buy Peacock Feathers' for row in rows),
        #       'Not located on this page')
        self.assertIn('1: Buy peacock feathers', [row.text for row in rows])

if __name__ == '__main__':
    unittest.main()

1 个答案:

答案 0 :(得分:0)

呃...这本书是TLDR!

第389页

Unexpected StaleElementException errors from Selenium often mean you have some kind of race condition. You should probably specify an explicit interaction/wait pattern.

它是固定的...

相关问题