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
.xpifile. You can get it from the Firefox Add-ons site or from the extension’s release page.
Step-by-Step
- Install Selenium and WebDriver Manager (optional but recommended):
Open a terminal and runpip install selenium webdriver-manager. The WebDriver Manager automatically handles the geckodriver binary, so you don’t have to download it manually. - Import required modules:
In your Python script, importwebdriverfrom selenium, and if using WebDriver Manager, importGeckoDriverManagerfromwebdriver_manager.firefox. Also importOptionsfromselenium.webdriver.firefox.options. - Create a Firefox Options object:
InstantiateOptions(). This allows you to set preferences and, importantly, to specify the path to the extension file. - Add the extension to the options:
Useoptions.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. - Initialize the WebDriver:
Create a driver instance. If using WebDriver Manager, usewebdriver.Firefox(options=options, service=Service(GeckoDriverManager().install())). If you have geckodriver in PATH, simplywebdriver.Firefox(options=options). - Verify the extension is installed (optional):
After the driver starts, you can navigate toabout:debugging#/runtime/this-firefoxto see the list of installed extensions, or check for a specific element that the extension adds to pages. - Run your test or automation:
Now you can proceed with your Selenium script. The extension will be active for the entire session. - Close the driver:
When done, calldriver.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_extensionmethod 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.enabledtoFalsein 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
- Android Chrome Dark Theme: How to Get It
- Restore WhatsApp Messages on Android: Full Guide
- Fix Quick Access Not Working in Windows 11
- Device Queue Not Busy Error in Windows 10: Fix Steps
- How to Pause and Disable Discord Invite Links
- WhatsApp Verification Code Not Received? Fixes That Work