Selenium2Library and Stale Element Reference Exception

1,709 views
Skip to first unread message

Tatu Aalto

unread,
Jul 28, 2015, 2:37:03 PM7/28/15
to robotframework-users
Ugh

Yesterday I did update to Selenium2Library 1.7.2, selenium 2.46.1 and when using Chrome (version 44.0.2403.107) with Chrome driver (version 2.16), the StaleElementReferenceException has started to happen quite often. Before the updates, it did happen quite rarely, perhaps once a week or so. But now, after the update, I see it few times in a day. What does worry me is that I get the StaleElementReferenceException also when I use the Wait Until Page Contains Element, Wait Until Element Is Visible and Wait Until Page Contains keywords. The exception is received before the timeout has reached and if recall the log correctly, I do receive the exception, when element is searched first time. 

Based on the selenium change log, I did not see any fixes or updates that could identify the issue. Also I did not spot any changes in the API documentation, but I do not read that so often to be able to spot any differences. 

Before I wonder any deeper to the investigation, I would like to know, has anyone else noticed this kind of exceptions to happen more frequently? And if they have, have they done any investigation on the matter and what might the findings might be? 


-Tatu  

Tatu Aalto

unread,
Jul 30, 2015, 5:21:17 AM7/30/15
to robotframework-users, aalto...@gmail.com
Ugh

I did do more investigation and it seems that selenium 2.46.0 upgrade did bring, at least for me, the StaleElementReferenceException. I also figured out why it happens, my test execution time, on selenium side did drop more than 50% (on Chrome browser, I do not gather statistics from other browsers regularly). The performance boost, which is a nice thing, did reveal some previous hidden timing issues and badly selected selectors. 

-Tatu

Tatu Aalto

unread,
Jul 30, 2015, 8:08:33 AM7/30/15
to robotframework-users, aalto...@gmail.com
Ugh

I did notice that I did forget to post the stack trace form the Wait* keywords. I agree that error is real and element in that moment is sale, but which I do not agree that why the Wait* keywords on Selenium2Library fails. In my mind, if there is an error, the Wait keywords should perform a retry. So is this a bug or is this expected behavior? In my mind this is a bug, because only way to cope on the error is extend the S2L with my own keywords, which will handle the situation or use Wait Until Keyword Succeeds. And both of them are quite a ugly hacks, which I do not like doing.   

Selenium2Library.Wait Until Element Is Visible xpath=//span/span[text()="${menu_item}"], timeout=5s, error=Menu item ${menu_item} not found on page!
Documentation:  
Waits until element specified with `locator` is visible.
Start / End / Elapsed:  20150730 14:24:49.623 / 20150730 14:24:50.240 / 00:00:00.617
00:00:00.376KEYWORD: common_library_imports.Selenium2library Common Failure
14:24:49.624    TRACE   Arguments: [ u'xpath=//span/span[text()="XXXXXX"]' | timeout=u'5s' | error=u'Menu item XXXXXX not found on page!' ]   
14:24:49.624    DEBUG   POST http://127.0.0.1:55614/session/7f76eac90d05725647286d5bdf089b4a/elements {"using": "xpath", "sessionId": "7f76eac90d05725647286d5bdf089b4a", "value": "//span/span[text()=\"XXXXXX\"]"}   
14:24:49.829    DEBUG   Finished Request    
14:24:49.831    DEBUG   GET http://127.0.0.1:55614/session/7f76eac90d05725647286d5bdf089b4a/element/0.4323001531884074-3/displayed {"sessionId": "7f76eac90d05725647286d5bdf089b4a", "id": "0.4323001531884074-3"}  
14:24:49.855    DEBUG   Finished Request    
14:24:50.239    FAIL    StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=44.0.2403.125)
  (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Windows NT 6.1 SP1 x86_64)
14:24:50.239    DEBUG   Traceback (most recent call last):
  File "<string>", line 2, in wait_until_element_is_visible
  File "C:\Python27\lib\site-packages\Selenium2Library\keywords\keywordgroup.py", line 15, in _run_on_failure_decorator
    return method(*args, **kwargs)
  File "C:\Python27\lib\site-packages\Selenium2Library\keywords\_waiting.py", line 128, in wait_until_element_is_visible
    self._wait_until_no_error(timeout, check_visibility)
  File "C:\Python27\lib\site-packages\Selenium2Library\keywords\_waiting.py", line 237, in _wait_until_no_error
    timeout_error = wait_func(*args)
  File "C:\Python27\lib\site-packages\Selenium2Library\keywords\_waiting.py", line 121, in check_visibility
    visible = self._is_visible(locator)
  File "C:\Python27\lib\site-packages\Selenium2Library\keywords\_element.py", line 723, in _is_visible
    return element.is_displayed()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 323, in is_displayed
    return self._execute(Command.IS_ELEMENT_DISPLAYED)['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 402, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\Selenium2Library\webdrivermonkeypatches.py", line 11, in execute
    result = self._base_execute(driver_command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 175, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)  
14:24:49.621    TRACE   Arguments: [ ${menu_item}=u'XXXXXX' | ${timeout}=u'10' ]

-Tatu 

Mark Winspear

unread,
Jul 30, 2015, 8:55:01 AM7/30/15
to robotframework-users, aalto...@gmail.com


I did notice that I did forget to post the stack trace form the Wait* keywords. I agree that error is real and element in that moment is sale, but which I do not agree that why the Wait* keywords on Selenium2Library fails. In my mind, if there is an error, the Wait keywords should perform a retry. So is this a bug or is this expected behavior? In my mind this is a bug, because only way to cope on the error is extend the S2L with my own keywords, which will handle the situation or use Wait Until Keyword Succeeds. And both of them are quite a ugly hacks, which I do not like doing.   

I agree. looks like a bug to me - the Wait keyword should be retrying 

Tatu Aalto

unread,
Jul 30, 2015, 9:03:15 AM7/30/15
to robotframework-users, markwi...@gmail.com
Reply all
Reply to author
Forward
0 new messages