Firefox connection refused when adding -profile argument to python selenium options

597 Views Asked by At

I'm attempting to add options to the startup of my Firefox selenium driver in python to capture crash data. The function I use to startup the driver looks like this:

import os 
from selenium import webdriver  
from selenium.webdriver.firefox.options import Options as FFOptions
from selenium.webdriver.firefox.webdriver import WebDriver as FirefoxWebDriver 
import tempfile
SELENIUM_TEMP_DIR = os.path.abspath('./data/raw')


def get_local_firefox_driver():
    
    os.environ['MOZ_FORCE_DISABLE_E10S'] = '1'
    ff_options = FFOptions()

    # Add custom crash dump folder
    profile_temp = tempfile.mkdtemp(".selenium", dir=SELENIUM_TEMP_DIR)
    print("*** Using profile: {}".format(profile_temp))
    ff_options.add_argument("-profile")
    ff_options.add_argument(profile_temp)

    ff_profile = webdriver.FirefoxProfile()
    # set some privacy settings
    ff_profile.set_preference("places.history.enabled", False)
    ff_profile.set_preference("privacy.clearOnShutdown.offlineApps", True)
    ff_profile.set_preference("privacy.clearOnShutdown.passwords", True)
    ff_profile.set_preference("privacy.clearOnShutdown.siteSettings", True)
    ff_profile.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
    ff_profile.set_preference("signon.rememberSignons", False)
    ff_profile.set_preference("network.cookie.lifetimePolicy", 2)
    ff_profile.set_preference("network.dns.disablePrefetch", True)
    ff_profile.set_preference("network.http.sendRefererHeader", 0)
    ff_profile.set_preference("permissions.default.image", 2)

    driver = webdriver.Firefox(firefox_profile=ff_profile, options=ff_options)
    return driver

When I attempt to startup the driver with this version of the function I get this error:

Traceback (most recent call last):
  File "/Users/username/project_name/error_demo.py", line 2, in <module>
    driver = get_local_firefox_driver(headless=False)
  File "/Users/username/project_name/src/selenium_helper.py", line 129, in get_local_firefox_driver
    driver = webdriver.Firefox(firefox_profile=ff_profile, options=ff_options, capabilities=ff_capabilities)
  File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: connection refused

During the crash, Firefox actually opens and looks likes this: enter image description here

If I comment out the section of code that is intended to capture the crash data

# Add custom crash dump folder
profile_temp = tempfile.mkdtemp(".selenium", dir=SELENIUM_TEMP_DIR)
print("*** Using profile: {}".format(profile_temp))
ff_options.add_argument("-profile")
ff_options.add_argument(profile_temp)

I get no errors and the Firefox browser opens up like this:

enter image description here

How does get_local_firefox_driver() need to be modified to get Firefox and geckodriver to startup correctly with the ability to capture crash data?

1

There are 1 best solutions below

2
AudioBubble On

I think something like this should work:

import tempfile

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# Custom profile folder to keep the minidump files
profile = tempfile.mkdtemp(".selenium")
print("*** Using profile: {}".format(profile))

# Use the above folder as custom profile
opts = Options()
opts.add_argument("-profile")
opts.add_argument(profile)
opts.binary = "/Applications/Firefox.app/Contents/MacOS/firefox"

driver = webdriver.Firefox(options=opts,
    # hard-code the Marionette port so geckodriver can connect
    service_args=["--marionette-port", "2828"])

# Your test code which crashes Firefox

If that didn't work I recommend you check out the official documentation