Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid a socket timeout for selenium webdriver?

I am having a complex python-selenium test suite to test a non-public webpage. In that setup I need to get the webdriver like follows:

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

with some profile and log path. In most cases this line of code just works fine, but sometimes (5% or the cases) I get a socket timeout error:

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

What could be a possible cause of this error? How to debug this error? How to fix it?

like image 479
Alex Avatar asked Jan 03 '18 10:01

Alex


1 Answers

The error says it all :

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

Sequence of events

Here is the sequence of events that happened :

  • Initially the error occurs at the following line in basicsuite.py file :

    self.driver = webdriver.Firefox(firefox_profile = profile, log_path = logfile)
    
  • After a series of checks finally the following method is called which fails :

    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
    
  • The program errors out at :

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

Conclusion

The conclusion is that the attempt to make the Connection wasn't successfull which means the initialization of the webdriver instance and further spawning of a new Mozilla Firefox Browser Session was unsuccessful.

Analysis

It won't be possible to conclude the real reason why timeout: timed out occured. But you can follow some of the Best Practices as follows :

  • Provide the complete name of the logfile along with the logical location of the logfile (from Project Level) as follows :

    self.driver = webdriver.Firefox(firefox_profile=profile, log_path='./Log/geckodriver.log')
    
  • Always use quit() in the tearDown() method so that the webdriver and the webclient both are properly destroyed.

  • Before starting your Test Execution, through Task Manager ensure that there are no dangling instances of GeckoDriver or Firefox process within your system.

  • Ensure that the binary versions you are using JDK, Selenium, GeckoDriver, Mozilla Firefox Browser are compatible. You can find a detailed discussion in this QA Selenium WebDriver 3.4.0 + geckodriver 0.18.0 + Firefox ?? - which combination works?

  • Clean the Project Workspace from your IDE before and after executing your Test Suite.

  • Use CCleaner tool regularly to wipe off the OS chores.

  • If the base version of Firefox Browser is too ancient, uninstall the Firefox Browser through Revo Uninstaller with Moderate Scan and install a recent GA-Released version of Firefox Browser.

like image 96
undetected Selenium Avatar answered Oct 23 '22 21:10

undetected Selenium