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

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


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).


A quick fix to try for anyone else out there struggling with this or similar errors -- I found that deleting my geckodriver.log file got rid of this error.

This is implied in the "Clean the Project Workspace" part of DebanjanB's answer, but I just wanted to share the concrete action that fixed it for me. Note that I was not using a test suite like the original poster.

I imagine what happened for me was that I had previous webdriver.Firefox instances that never got to driver.close() in my code due to an error in another part of my code, which I was still debugging, and they had not released the log file geckodriver.log.

Thus I imagine this issue might also be solved by renaming the log file or writing to a different log file.