6

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?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • What happens before? Probably you just shut down the browser and it's still shutting down and now doesn't want to come up again? – Daniel Alder Jan 03 '18 at 11:54
  • Probably. I shut it down with `stop_client` and `close`. Maybe I need a `sleep`? – Alex Jan 03 '18 at 12:11
  • I don't know. I don't actively work with selenium. But our tests (Java) call `driver.close(); driver.quit();` at the end. maybe this helps – Daniel Alder Jan 03 '18 at 12:29

3 Answers3

3

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks for the answer - but I do not understand the conclusion. What connection was not successfull? The connection of selenium to its driver? Also, following these suggestions, the error still persists, at a level of maybe 2%. Maybe put the webdriver code in a while-try-except loop? – Alex Jan 16 '18 at 12:31
  • Updated my Answer with some more inputs. Now `webdriver` being a **W3C Recomendation Candidate** and the initialization and destroy process being **much cleaner** I don't see any necessity to wrap up the `initialization` in a `while-try-except loop`. We are in a position to deal with each error outcome. – undetected Selenium Jan 16 '18 at 16:43
  • Faced same small percentage of errors too. Machines recover after 5 minutes with no intervention, but that causes 1 test to fail in batch of ~200. I have asked about how to set a connection timeout, so we can retry more explicitly, and gotten no answer, since I need to try/catch and retry. Because, sometimes, a test teardown() will just crash, that is just life. Windows/Ubuntu/Raspbian/Mojave #selenium3 –  Jun 11 '21 at 12:46
0

This happened to me in my Django app. The problem went away when I set DEBUG = False in my settings.py file.

Raffi
  • 970
  • 11
  • 13
0

it's probably not related but

if you are using vs-code on windows and you got this error just try to run from cmd, not from vs-code

Kaan
  • 23
  • 4
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '22 at 06:59