Reputation: 44325
I am using a very complex setup to test various non-public webpages. I use jenkins
to run the python-selenium
tests within a docker
image. 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:
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:
Addendum:
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?Upvotes: 20
Views: 23860
Reputation: 16121
I had the same problem, and found that it was a permissions problem. I am running Selenium within apache, and the apache folder did not have the correct permissions. Either selenium, geckodriver, or firefox tries creating files and folders in the /var/www/
directory, and it does not have permissions to do so.
You can fix this with the following command:
chmod a+rwx /var/www/
The above command will work, but is potentially insecure as it gives everyone access to the folder. If you want to make it a bit more secure, try:
chown www-data /var/www/
This are both quick hacks, the better way would be to figure out why geckodriver is creating files in the folder, and change that folder setting (I haven't done this).
Upvotes: 0
Reputation: 61
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.
Upvotes: 3
Reputation: 193088
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 :
GeckoDriver
or Marionette
by the new session which previous session have't released yet.Docker Image
ran out of memoryHere you can find a similar Discussion
.
As per the above mentioned causes, you need to follow a few steps as follows :
Selenium-Python
client, WebDriver variant
(GeckoDriver
) and Web Browser
(Firefox Browser
)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
CCleaner
tool regularly to wipe away the OS chores including the stale rust_mozprofile directories
.Upvotes: 8
Reputation: 473863
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).
Upvotes: 5