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.

Leave a Comment