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

时间:2018-01-03 10:48:56

标签: python selenium

我正在使用复杂的python-selenium测试套件来测试非公共网页。在那个设置中,我需要得到如下的webdriver:

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

可能导致此错误的原因是什么?如何调试此错误?怎么解决?

2 个答案:

答案 0 :(得分:2)

错误说明了一切:

  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.
    

结论

结论是尝试使Connection无法成功,这意味着webdriver实例的初始化并进一步产生新的Mozilla Firefox浏览器会议没有成功。

分析

无法确定 timeout: timed out 发生的真正原因。但您可以按照以下方式遵循一些最佳实践:

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

    self.driver = webdriver.Firefox(firefox_profile=profile, log_path='./Log/geckodriver.log')
    
  • 始终在 tearDown()方法中使用 quit() ,以便 webdriver webclient 都被正确销毁。

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

  • 确保您使用的二进制版本 JDK Selenium GeckoDriver Mozilla Firefox Browser 兼容。您可以在此质量检查Selenium WebDriver 3.4.0 + geckodriver 0.18.0 + Firefox ?? - which combination works?

  • 中找到详细讨论 在执行Project Workspace之前和之后,
  • 清除 IDE中的Test Suite

  • 定期使用 CCleaner 工具擦除关闭操作系统。

  • 如果Firefox Browser的基本版本过于古老,请使用Firefox Browser通过 Revo Uninstaller 卸载Moderate Scan并安装最近版本GA-Released的{​​{1}}版本。

答案 1 :(得分:0)

这在我的Django应用中发生了。当我在DEBUG = False文件中设置settings.py时,问题消失了。