python - 如何处理 python WebDriver 中的零星 BadStatusLine、CannotSendRequest 错误

标签 python selenium jenkins selenium-webdriver webdriver

自从我们开始在 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)

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

  • Selenium ==2.44.0
  • python==2.7.3
  • 火狐==34.0
  • Jenkins
  • xvfb(使用 jenkins 插件进行 headless 显示)

虽然我们已经看到这些错误在 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 或类似的东西吗?

最佳答案

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

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 在后台杀死我们的浏览器。

关于python - 如何处理 python WebDriver 中的零星 BadStatusLine、CannotSendRequest 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530823/

相关文章:

jenkins - Kubernetes上的Jenkins-无法使用工作空间访问工作目录

python - 生成倒排索引

python - 为什么 torch.gt 函数将 requires_grad 变成 False?

python - 如何通过 Selenium python 登录到 yahoo(尝试了多种解决方案)

jenkins - 如何通过 REST API 创建 jenkins 凭据?

variables - Jenkins 插件 - 环境变量

python - 在 GUI 内打开指南

python - 在 Windows 上模拟/dev/random

javascript - 如何使用 selenium 获取所有元素的直接子元素和 css 选择器?

python - 无法将 send_keys 发送到工具提示所覆盖的网络元素