Splitter is_text_present()导致Firefox出现间歇性StaleElementReferenceException(但不是Chrome或phantomjs)

2024-06-09 13:53:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在将我的测试环境升级到seleniumsplinter和Firefox的最新版本之后,我的一个测试现在在使用Firefox时失败了大约80%,原因如下:

Traceback (most recent call last):
  File "/Users/rbednark/Dropbox/git/quizme_website/questions/tests.py", line 103, in test_login_fails_incorrect_username
    "Your username and password didn't match. Please try again."
  File "/Users/rbednark/.virtualenvs/quizme-django-1.6.5/lib/python2.7/site-packages/splinter/driver/webdriver/__init__.py", line 302, in is_text_present
    self.driver.find_element_by_tag_name('body').text.index(text)
  File "/Users/rbednark/.virtualenvs/quizme-django-1.6.5/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 73, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "/Users/rbednark/.virtualenvs/quizme-django-1.6.5/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "/Users/rbednark/.virtualenvs/quizme-django-1.6.5/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/Users/rbednark/.virtualenvs/quizme-django-1.6.5/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.

请注意,使用Chrome和phantomjs同样的测试100%都能通过。测试:

^{pr2}$

firefox从49.0.2升级到50.1.0
硒从2.46.0升级到{}
碎片从0.6.0升级到0.7.5
壁虎0.13.0
(以上均为发布时的最新版本)


Tags: djangotextinpylibpackagesseleniumline
1条回答
网友
1楼 · 发布于 2024-06-09 13:53:44

我通过实现这个包装器解决了我的问题,当捕获异常时重试:

from selenium.common.exceptions import StaleElementReferenceException

def _loop_is_text_present(text, max_attempts=3):
    attempt = 1
    while True:
        try:
            return self.browser.is_text_present(text)
        except StaleElementReferenceException:
            if attempt == max_attempts:
                raise
            attempt += 1

灵感来源:http://darrellgrainger.blogspot.com/2012/06/staleelementexception.html

硒源代码describes the StaleElementReferenceException.

相关问题 更多 >