【问题标题】:Selenium webdriver crashing Firefox during unittestsSelenium webdriver 在单元测试期间使 Firefox 崩溃
【发布时间】:2018-06-10 14:02:44
【问题描述】:

当通过 Selenium 从一些 Django 单元测试中运行 Firefox 时,您如何确定为什么会崩溃?

我的测试用例是:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from pyvirtualdisplay import Display
from selenium import webdriver

class Tests(StaticLiveServerTestCase):

    def setUp(self):
        super(Tests, self).setUp()

        self.vdisplay = Display(visible=0, size=(1920, 1080), backend='xvfb')
        self.vdisplay.start()

        profile = webdriver.FirefoxProfile()
        log_path = '/tmp/tests.log'
        self.driver = webdriver.Firefox(profile, log_path=log_path)

    def test_abc(self):
        blah

当我在无头服务器上运行它时:

python manage.py test functional_tests --nomigrations --failfast

它几乎立即出错:

ERROR: test_abc (myproject.functional_tests.tests.Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/src/myproject/functional_tests/tests.py", line 15, in setUp
    self.driver = webdriver.Firefox(profile, log_path=log_path)
  File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__
    keep_alive=True)
  File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Process unexpectedly closed with status: 1

所以webdriver 在初始化 Firefox 实例时遇到了问题。但是,日志文件只显示:

1528498122788   geckodriver INFO    geckodriver 0.19.0
1528498122799   geckodriver INFO    Listening on 127.0.0.1:39255
1528498123950   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.LrTWF7H6fk2y"

我的版本是:

Ubuntu Version is 16.04
Selenium Version is 3.8.1
Geckodriver Version is 0.19.0
Firefox Version is 58.0.1

我尝试升级到最新的 Selenium 3.12、Geckodriver 0.20.1 和 Firefox 60,但我得到了完全相同的错误。

我也尝试运行所有系统包升级,然后重新启动,但没有效果。

我该如何解决这个问题,或者至少获得关于 Firefox 崩溃原因的更好的错误消息?

编辑:这与this question 不同,后者返回的错误消息非常不同。它的解决方案不能解决我的错误。

【问题讨论】:

    标签: python selenium selenium-webdriver geckodriver


    【解决方案1】:

    首先,也许您应该通过以下方式启用trace log

    capabilities = DesiredCapabilities.FIREFOX.copy()
    capabilities["moz:firefoxOptions"] = {
        "log": {
            "level": "trace",
        },
    }
    driver = webdriver.Firefox(capabilities=capabilities)
    

    或者

    opts = Options()
    opts.log.level = "trace"
    driver = webdriver.Firefox(firefox_options=opts)
    

    其次,据我所知,这个问题通常是由图形环境要求引起的。似乎如果您没有在具有有效DISPLAY 的会话中运行firefox 实例,它将以代码1 退出。

    所以也许您的问题可以通过以下方式解决:

    options = Options()
    profile = webdriver.FirefoxProfile()
    options.add_argument("--headless")
    driver = webdriver.Firefox(profile, firefox_options=options)
    

    最后,我必须说我实际上不确定您的问题,因为信息有限。

    【讨论】:

    • --headless 选项成功了。我正在使用xvfb,所以我不确定为什么没有它它会崩溃,但现在它正在工作。谢谢!
    • 在使用xvfb 时,您仍然需要在启动脚本时手动设置DISPLAY 环境变量。也许是因为这个?
    • 我知道。这就是我的代码中的pyvirtualdisplay 应该做的事情。这个设置已经为我工作了几个月,然后在更新后发生了一些变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2017-06-18
    • 2016-01-02
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多