tack tack - 9 days ago 6
Python Question

Python Selenium stale element exception

I study Django and TDD. There is code (functional test) which verifies entry form to be located in center.

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class NewVisitorTest(LiveServerTestCase):
"""
Test Class
"""
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)

def tearDown(self):
self.browser.quit()

def test_layout_and_styling(self):
self.browser.get(self.live_server_url)
self.browser.set_window_size(1024, 768)

inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual(
inputbox.location['x'] + inputbox.size['width'] / 2,
512, delta=10
)

inputbox.send_keys('testing\n')
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual(
inputbox.location['x'] + inputbox.size['width'] / 2,
512, delta=10
)


The error occurs:

======================================================================
ERROR: test_layout_and_styling (functional_tests.tests.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "e:\Education\ttd\superlists\functional_tests\tests.py", line 112, in tes
t_layout_and_styling
inputbox.location['x'] + inputbox.size['width'] / 2,
File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\we
belement.py", line 358, in location
old_loc = self._execute(Command.GET_ELEMENT_LOCATION)['value']
File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\we
belement.py", line 448, in _execute
return self._parent.execute(command, params)
File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\we
bdriver.py", line 196, in execute
self.error_handler.check_response(response)
File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\er
rorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale elemen
t reference: element is not attached to the page document
(Session info: chrome=44.0.2403.155)
(Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a9468
2a),platform=Windows NT 6.1 SP1 x86_64)


With Firefox it works OK.
I tried also:

from selenium.webdriver.support.ui import WebDriverWait
...
...
def find(driver):
element = driver.find_elements_by_id("data")
if element:
return element
else:
return False
element = WebDriverWait(driver, secs).until(find)


Also, I tryed: self.browser.implicitly_wait(3), but it does not help. I have the latest updates of Selenium and ChromeDriver. Please, advise what is wrong with it?

I read similar questions on stackoverflow but it also does not help.
Thank you!

Answer

inputbox.send_keys('testing\n')

I would like to bet that your form is responding the newline and submitting itself. So by the time you do the assertion the page displayed in the browser has changed or is in the process of being changed.

Comments