sodcalculator

10 Tech How-To

Install Firefox Extensions with Selenium in Python

Guide 10-9167 · Updated · Step by step

This guide shows you how to automate the installation of Firefox extensions (also called add-ons) using Selenium WebDriver in Python. This is useful for testing scenarios where you need a specific extension active before your browser session starts, or when you need to set up multiple browser profiles with the same set of add-ons. You’ll need Python installed, along with the Selenium package and a Firefox browser on your system.

What You’ll Need

  • Python 3.6 or newer installed on your machine.
  • The Selenium package installed via pip (pip install selenium).
  • Mozilla Firefox installed (any recent version).
  • WebDriver for Firefox – either geckodriver (automatically managed by Selenium 4.6+) or manually downloaded and placed in your PATH.
  • The extension file you want to install, usually a .xpi file. You can get it from the Firefox Add-ons site or from the extension’s release page.

Step-by-Step

  1. Install Selenium and WebDriver Manager (optional but recommended):
    Open a terminal and run pip install selenium webdriver-manager. The WebDriver Manager automatically handles the geckodriver binary, so you don’t have to download it manually.
  2. Import required modules:
    In your Python script, import webdriver from selenium, and if using WebDriver Manager, import GeckoDriverManager from webdriver_manager.firefox. Also import Options from selenium.webdriver.firefox.options.
  3. Create a Firefox Options object:
    Instantiate Options(). This allows you to set preferences and, importantly, to specify the path to the extension file.
  4. Add the extension to the options:
    Use options.add_extension('/full/path/to/your/extension.xpi'). Make sure the path is absolute. You can add multiple extensions by calling this method multiple times.
  5. Initialize the WebDriver:
    Create a driver instance. If using WebDriver Manager, use webdriver.Firefox(options=options, service=Service(GeckoDriverManager().install())). If you have geckodriver in PATH, simply webdriver.Firefox(options=options).
  6. Verify the extension is installed (optional):
    After the driver starts, you can navigate to about:debugging#/runtime/this-firefox to see the list of installed extensions, or check for a specific element that the extension adds to pages.
  7. Run your test or automation:
    Now you can proceed with your Selenium script. The extension will be active for the entire session.
  8. Close the driver:
    When done, call driver.quit() to close the browser and release resources.

Common Mistakes

  • Using a Chrome extension (.crx) file: Firefox uses .xpi files, not .crx. Ensure you download the Firefox version of the extension.
  • Relative paths: The path to the .xpi file must be absolute. Using a relative path often results in a file-not-found error.
  • Forgetting to install geckodriver: If you don’t use WebDriver Manager, geckodriver must be in your PATH. Otherwise, Selenium will throw a WebDriverException.
  • Adding the extension after the browser starts: The add_extension method must be called on the Options object before creating the driver. Adding it after won’t work.
  • Using an outdated Selenium version: Some older versions have issues with Firefox extensions. Update Selenium to the latest version.

Tips

  • Use a fresh profile: By default, Selenium uses a temporary profile. This is fine for testing, but if you need persistent settings, you can specify a profile using options.profile.
  • Disable auto-updates: To avoid extension updates during your test, set the preference extensions.update.enabled to False in the options.
  • Check extension compatibility: Some extensions may not work with the latest Firefox version. Test your script with a compatible version.
  • Headless mode: If you’re running in headless mode, some extensions that rely on UI elements may not work as expected. Test in normal mode first.
  • Use WebDriver Manager: It simplifies geckodriver management and reduces setup errors.

Troubleshooting

  • Extension not loading: Check that the .xpi file is not corrupted. Try installing it manually in Firefox to confirm it works. Also, ensure the file path is correct and the file is readable.
  • WebDriverException: ‘geckodriver’ executable needs to be in PATH: Install geckodriver manually and add it to your PATH, or use WebDriver Manager as shown above.
  • SessionNotCreatedException: This often happens when Firefox and geckodriver versions are incompatible. Update both to the latest versions.
  • Extension appears disabled: Some extensions are disabled by default in private browsing or headless mode. Check the extension’s settings and your browser options.
  • Slow startup: If the extension is large, the browser may take longer to start. Increase your script’s implicit wait or use explicit waits for elements that appear after the extension loads.

Frequently Asked Questions

Can I install extensions that are not in .xpi format?

Firefox extensions are typically packaged as .xpi files. Some are also available as ZIP files; you can rename the .zip to .xpi and it should work, as long as the internal structure is correct.

How do I get the .xpi file for an extension?

Visit the extension’s page on the Firefox Add-ons site. Look for a “Download” or “Add to Firefox” button. You can often right-click and save the link as .xpi, or use a tool like DownThemAll to capture it. Alternatively, some developers provide direct .xpi links on their sites.

Will the extension persist across sessions?

No, by default Selenium uses a temporary profile that is deleted after the driver quits. If you need persistence, you must specify a custom profile using options.profile.

Can I install multiple extensions at once?

Yes, simply call options.add_extension() multiple times with different file paths. All will be installed when the browser starts.

Does this work with Firefox for Android?

No, this method is for desktop Firefox only. Selenium WebDriver for Android uses a different mechanism and does not support installing extensions in the same way.

Why do I get an error about ‘add_extension’ not being available?

Make sure you are using the Options class from selenium.webdriver.firefox.options, not the generic Options from selenium.webdriver.common.options. The Firefox-specific class has the add_extension method.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →