How to fix "WebDriverException: Message: connection refused"?

I am using a very complex setup to test various non-public webpages. I use jenkins to run the python-selenium tests within a dockerimage. That way, I am completly independent of the jenkins environment and can create my own environment. In this environment I have the following software installed:

  • Firefox: 57.0.1
  • geckodriver: 0.18.0
  • nosetests: 1.3.7
  • selenium: 3.8.0

The selenium tests create the WebDriver the following way:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir", self.downloadpath)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.useDownloadDir", True)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
"application/force-download, image/png, text/html, text/plain, "
"image/tiff, text/csv, application/zip, application/octet-stream")
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.closeWhenDone", True)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting",
False)
self.driver = webdriver.Firefox(profile, log_path = logfile)

where logfile and self.downloadpath are two valid paths in the docker setup.

The whole test suite consists of 6 independant test cases, each with the same setup as above. They normally run fine and complete without problems.

But without any change to the tests or the general setup, a test sometimes fails with the following error message:

  File "/root/tests/bsp_usecase_tests/tools/basicsuite.py", line 210, in set_driver_firefox
    self.driver = webdriver.Firefox(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 311, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: connection refused

I have two questions:

  1. What connection is refused here? What is the meaning of the error message
  2. How can I possibly fix this error?

Addendum:

  • When I used a time.sleep(10) just before the webdriver.Firefox line, the error did not show up anymore. Shoud I put that line in a while-try-except loop?

The error you are seeing is :

WebDriverException: Message: connection refused

As per the documentation WebDriverException is the Base webdriver exception which is as follows :

exception selenium.common.exceptions.WebDriverException(msg=None, screen=None, stacktrace=None)

So connection is refused here means that Selenium is unable to establish the connecting which you wanted to establish through :

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

A possible solution would be to 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')

Here you can find a similar Discussion

Again, as you mentioned When I used a time.sleep(10) just before the webdriver.Firefox line, the error did not show up anymore, so I assume there was an instance of GeckoDriver and Firefox Browser client active previously. Hence, similarly as @Florent B. mentioned you have to shield your script against facing Race Around Condition which can stem out from either of the following :

  • Accessing the same logfile by the new session which previous session have't released yet.
  • Accessing the same port number by GeckoDriver or Marionette by the new session which previous session have't released yet.
  • Lack of access to CPU
  • Lack of Physical Memory
  • Lack of Swap Memory
  • Lack of Disc Cache
  • Lack of Network Bandwidth
  • Docker Image ran out of memory

Here you can find a similar Discussion.

As per the above mentioned causes, you need to follow a few steps as follows :

  • Always use the latest released version of Selenium-Python client, WebDriver variant (GeckoDriver) and Web Browser (Firefox Browser)
  • Always use quit() in the tearDown() method so that the webdriver and the webclient both are properly destroyed.
  • Clean the Project Workspace from your IDE before and after executing your Test Suite.
  • Clear the Browser Cache before and after the execution of your Tests
  • Use CCleaner tool regularly to wipe away the OS chores including the stale rust_mozprofile directories.

What connection is refused here? What is the meaning of the error message

The connection between the Python webdriver API and your Firefox web browser. Well, not the connection itself, but a single request that the webdriver sent was "rejected" by the browser. Note that this works through the JSON Wire protocol - JSON over HTTP.

How can I possibly fix this error?

Usually, in case of error like this, the most common reason is a compatibility issue. In other words, I suspect that your geckodriver version 0.18.0 is too old for the Firefox 57. Upgrade geckodriver to the latest stable version (currently 0.19.1).