python - 为什么 split is_text_present() 会导致 Firefox 间歇性出现 StaleElementReferenceException(但 Chrome 或 phantomjs 不会)?

标签 python selenium firefox splinter

将我的测试环境升级到最新版本 selenium 后, splinter和 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% 成功。测试:

self.browser.visit(self.live_server_url)
self.assertEquals(self.browser.title, 'Quiz Me!')
self.browser.find_by_id('id_username')[0].fill(user)
self.browser.find_by_id('id_password')[0].fill(password)
# note that the following click() causes a page load
self.browser.find_by_value('login').click()
self.assertTrue(
      self.browser.is_text_present(
          "Your username and password didn't match. "
          "Please try again."
  )
)

Firefox 从 49.0.2 升级到 50.1.0
selenium 从 2.46.0 升级到 3.0.2
splinter从0.6.0升级到0.7.5
geckodriver 0.13.0
(以上均为发布时的最新版本)

最佳答案

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

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

Selenium 源代码 describes the StaleElementReferenceException.

关于python - 为什么 split is_text_present() 会导致 Firefox 间歇性出现 StaleElementReferenceException(但 Chrome 或 phantomjs 不会)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539231/

相关文章:

Python 速度差异

google-chrome - 从 Eclipse IDE 调用 Selenium 脚本时出现 "Chrome Not Reachable"

javascript - 在 JavaScript 中转换日期在 Firefox 中不起作用

python - 用一维变量填充 3D numpy 数组

php - 希望迁移到 Python+Django 的普通 PHP 开发人员 : Directly go with Django or learn the MVC Framework stuff in PHP first?

selenium - 如何使用 selenium java 在 mozilla firefox 上使用假网络摄像头

java - 如何使用 TestNG 和 Java 设置方法执行期间的调用计数

javascript - 为什么我的网站会加载包含旧内容的 js 文件?

javascript - 为什么 setTimeout(.., 0) 在 Firefox 中的浏览器操作之前触发?漏洞?

python - 将列合并为一列