pythonseleniumselenium-webdriverfirefoxgecko

Firefox Webdriver Install Addon to Remote Webdriver


I have the following code that connects to a Remote Webdriver and installs an extension

options = webdriver.FirefoxOptions()
options.set_preference('intl.accept_languages', 'en,en-US')
options.add_argument('--log-level=3')  # Not logs will be displayed.
options.add_argument('--mute-audio')  # Audio is muted.
options.add_argument('--enable-webgl-draft-extensions')
options.add_argument('--disable-infobars')  # Disable popup
options.add_argument('--disable-popup-blocking')  # and info bars.

profile = webdriver.FirefoxProfile()
profile.add_extension('/path/to/tampermonkey.xpi')

driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options, browser_profile=profile)

But when I go into the browser, the extension was never installed. Am I misunderstanding how to install extension in geckodriver?


Solution

  • For Firefox, you should not use add_extension, as mentioned in this issue:

    the currently supported approach now is to add the extension from the install_addon() method on the firefox driver after the session has been created.

    However, install_addon is only available for local webdrivers. A simple workaround is required when using remote webdrivers, as mentioned in this issue. The trick requires you to change:

    profile = webdriver.FirefoxProfile()
    profile.add_extension('/path/to/tampermonkey.xpi')
    
    driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options, browser_profile=profile)
    

    to

    driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options)
    addon_id = webdriver.Firefox.install_addon(driver, "/path/to/tampermonkey.xpi")
    

    The full code should be something like below:

    from selenium import webdriver
    
    options = webdriver.FirefoxOptions()
    options.set_preference('intl.accept_languages', 'en,en-US')
    options.add_argument('--log-level=3')  # Not logs will be displayed.
    options.add_argument('--mute-audio')  # Audio is muted.
    options.add_argument('--enable-webgl-draft-extensions')
    options.add_argument('--disable-infobars')  # Disable popup
    options.add_argument('--disable-popup-blocking')  # and info bars.
    
    driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options)
    addon_id = webdriver.Firefox.install_addon(driver, "/path/to/tampermonkey.xpi")
    
    # The add-on is installed
    
    # and optionally uninstall the add-on by uncommenting the code below
    # webdriver.Firefox.uninstall_addon(driver, addon_id)
    

    I have opened a pull request to the Selenium Docs to clarify such usages.