【问题标题】:How to deal with sporadic BadStatusLine, CannotSendRequest errors in python WebDriver如何处理 python WebDriver 中零星的 BadStatusLine、CannotSendRequest 错误
【发布时间】:2015-02-16 07:26:45
【问题描述】:

自从我们开始在 jenkins 中运行 selenium UI 测试以来,我们注意到测试期间出现的错误频率虽然很小但很烦人。我们在看似随机的 selenium 操作(单击、退出、访问等)上收到 BadStatusLine 和 CannotSendRequest 错误。

它们通常看起来像:

  File "/usr/lib/python2.7/unittest/case.py", line 327, in run
    testMethod()
  File "/home/jenkins/workspace/Create and Upload Functional Testing/shapeways/test_suite/Portal/CreateAndUpload/TestUploadWhenNotLoggedIn_ExpectLoginModal.py", line 22, in runTest
    self.dw.visit(ShapewaysUrlBuilder.build_model_upload_url())
  File "/home/jenkins/workspace/Create and Upload Functional Testing/webdriver/webdriverwrapper/WebDriverWrapper.py", line 212, in visit
    return self.execute_and_handle_webdriver_exceptions(lambda: _visit(url))
  File "/home/jenkins/workspace/Create and Upload Functional Testing/webdriver/webdriverwrapper/WebDriverWrapper.py", line 887, in execute_and_handle_webdriver_exceptions
    return function_to_execute()
  File "/home/jenkins/workspace/Create and Upload Functional Testing/webdriver/webdriverwrapper/WebDriverWrapper.py", line 212, in <lambda>
    return self.execute_and_handle_webdriver_exceptions(lambda: _visit(url))
  File "/home/jenkins/workspace/Create and Upload Functional Testing/webdriver/webdriverwrapper/WebDriverWrapper.py", line 205, in _visit
    return self.driver.get(url)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 185, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 171, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 380, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1030, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)

这个特殊情况来自以下堆栈:

  • 硒==2.44.0
  • python==2.7.3
  • 火狐==34.0
  • 詹金斯
  • xvfb(使用 jenkins 插件进行无头显示器)

尽管我们已经看到这些错误在 firefox/selenium 的许多不同版本排列中一直出现。

我运行了一个 tcpdump 来捕获在 BadStatusLine 错误出现之前发送的实际请求并得到以下信息。

    POST /hub/session/ab64574a-4a17-447a-b2e8-5b0f5ed5e923/url HTTP/1.1 
    Host: 127.0.0.1:41246
    Accept-Encoding: identity Content-Length: 102
    Connection: keep-alive
    Content-type: application/json;charset="UTF-8"
    POST: /hub/session/ab64574a-4a17-447a-b2e8-5b0f5ed5e923/url
    Accept: application/json
    User-Agent: Python http auth

    {"url": "http://example.com/login", "sessionId": "ab64574a-4a17-447a-b2e8-5b0f5ed5e923"}

响应返回 0 个字节。所以 BadStatusLine 是由空响应引起的,这是有道理的。

问题是,为什么 selenium 的服务器会返回一个空响应。如果服务器死了,我们不会得到 ConnectionError 或类似的东西吗?

【问题讨论】:

    标签: python selenium jenkins selenium-webdriver webdriver


    【解决方案1】:

    有一段时间,我没有重现,也不知道原因是什么。我终于能够通过运行来重现:

    import requests
    import json
    
    while True:
    
      requests.post('http://127.0.0.1/hub/session/', data=json.dumps({"url": "http://example.com/login", "sessionId": "ab64574a-4a17-447a-b2e8-5b0f5ed5e923"}))
    

    在运行时,我退出了浏览器并收到了 BadStatusLine 错误!当我再次尝试发出该请求时,我得到了预期的“ConnectionError”,你会从任何死服务器上看到它。

    所以,我怀疑会发生什么,当浏览器被发送终止信号时,在其关闭期间有一个短暂的窗口,仍然会返回任何响应,但只有 0 个字节。这就是为什么您会为基本相同的问题(浏览器死机)获得不同类型的异常。原来我们有一个 cron 在后台杀死我们的浏览器。

    【讨论】:

    • 你是怎么解决的?你如何防止 chron 这样做?
    • @deweydb 有人在 crontab 中添加了一行以查找超过某个时间的浏览器并终止该进程。我刚刚删除了那条线,事情进展顺利。不过我的情况很具体。但根本问题是普遍存在的:浏览器在请求中死掉。
    • 我知道我参加聚会已经很晚了,但我想我会补充一点,如果有人开始在以前工作的代码上遇到这个问题,那么请尝试降级您的 Firefox 版本。我开始在 Firefox 47 上遇到这个问题(我想是因为浏览器崩溃了)。降级到 Firefox 45 修复了它。
    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2018-12-12
    相关资源
    最近更新 更多