python - 如何避免 selenium webdriver 的套接字超时?

标签 python selenium

我有一个复杂的 python-selenium 测试套件来测试非公开网页。在该设置中,我需要像下面这样获取网络驱动程序:

self.driver = webdriver.Firefox(firefox_profile = profile, log_path = logfile)

带有一些配置文件和日志路径。在大多数情况下,这行代码工作正常,但有时(5% 或这种情况)我会收到套接字超时错误:

File "/root/tests/usecase_tests/tools/basicsuite.py", line 213, in set_driver_firefox
    self.driver = webdriver.Firefox(firefox_profile = profile, log_path = logfile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__
    keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 309, 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 460, 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 484, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1136, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 409, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/lib/python2.7/socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
timeout: timed out

此错误的可能原因是什么?如何调试这个错误?如何解决?

最佳答案

错误说明了一切:

  File "/usr/lib/python2.7/socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
timeout: timed out

事件顺序

这是发生的事件顺序:

  • 最初错误发生在 basicsuite.py 中的以下行文件:

    self.driver = webdriver.Firefox(firefox_profile = profile, log_path = logfile)
    
  • 经过一系列检查,最终调用了以下方法,但失败了:

    def readinto(self, b):
    """Read up to len(b) bytes into the writable buffer *b* and return
    the number of bytes read.  If the socket is non-blocking and no bytes
    are available, None is returned.
    
    If *b* is non-empty, a 0 return value indicates that the connection
    was shutdown at the other end.
    """
    self._checkClosed()
    self._checkReadable()
    if self._timeout_occurred:
        raise OSError("cannot read from timed out object")
    while True:
        try:
        return self._sock.recv_into(b)
        except timeout:
        self._timeout_occurred = True
        raise
        except error as e:
        if e.args[0] in _blocking_errnos:
            return None
        raise
    
  • 程序错误出现在:

    self._sock.recv(b) # where If *b* is non-empty, a 0 return value indicates that the connection was shutdown at the other end.
    

结论

结论是建立连接的尝试没有成功,这意味着 webdriver 的初始化实例并进一步生成新的 Mozilla Firefox浏览器 session 不成功。

分析

不可能得出 timeout: timed out 的真正原因。 发生。但您可以遵循以下一些最佳实践:

  • 提供日志文件的完整名称以及 logical location 日志文件(来自项目级别)如下:

    self.driver = webdriver.Firefox(firefox_profile=profile, log_path='./Log/geckodriver.log')
    
  • 始终使用 quit() tearDown() 方法中,以便 webdriver webclient 两者均已妥善销毁。

  • 在开始您的 Test Execution 之前, 通过 Task Manager确保没有悬挂实例 GeckoDriver Firefox 在您的系统中处理。

  • 确保您使用的二进制版本 JDK , Selenium , GeckoDriver , Mozilla Firefox Browser 兼容。您可以在此 QA Selenium WebDriver 3.4.0 + geckodriver 0.18.0 + Firefox ?? - which combination works? 中找到详细讨论

  • 清理 Project Workspace在执行 Test Suite 之前和之后从您的 IDE .

  • 使用 CCleaner 工具定期清除 操作系统杂务。

  • 如果基本版本Firefox Browser太古老了,卸载Firefox Browser通过 Revo Uninstaller Moderate Scan并安装最近的 GA-Released Firefox Browser 的版本.

关于python - 如何避免 selenium webdriver 的套接字超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48075864/

相关文章:

java - 使用 CSS 类名的 Xpath 或使用 CSS 获取定位器

java - Selenium WebDriver jQuery

python - Django 1.3,如何像在 ebay 上一样在帖子结束时发出信号?

python - PyMC:利用 Adaptive Metropolis MCMC 中的稀疏模型结构

python - 配置 django settings.TIME_ZONE 会影响 datetime.datetime.now() 吗?

java - 如何使用java查找并列出selenium中ul li标签下的元素和ul li位于div下的元素?

使用没有元素 id 的 Selenium 测试 Angularjs

python - 设置在使用 BlobServiceClient 创建的 Azure Blob 容器中的生存时间

python - 椭圆曲线点乘有时会产生错误的结果

java - 如何改善appium中点击之间的时间?