Skip to main content
SeleniumDecoded

Install a Browser Extension in a Test Session

Load a Firefox add-on at runtime or a Chrome extension at startup, find its ID, uninstall it again, and know the limits of branded Chrome builds.

Medium Verified against Selenium 4.52.0

Problem

You are testing a browser extension (or an app that behaves differently when one is present, such as an ad blocker or password manager). You need the extension loaded in the Selenium-controlled browser, ideally without building a custom profile by hand.

Solution

Firefox installs add-ons into a running session, so you can add and remove them per test. Chrome and Edge load extensions at startup through options. Since Selenium 4.50, the old Firefox profile extension methods are gone in every binding; use the driver-level install shown here (or BiDi webExtension.install).

Firefox: install at runtime

Install and uninstall a Firefox add-on
Selenium 4 Stable
FirefoxDriver firefox = new FirefoxDriver();
// Signed .xpi, or an unpacked directory / unsigned .xpi as a temporary add-on
String id = firefox.installExtension(Path.of("extensions/my-addon.xpi"), true);
firefox.get("https://app.example.com");
// ... assertions with the add-on active ...
firefox.uninstallExtension(id);
driver = webdriver.Firefox()
# temporary=True allows unsigned add-ons and unpacked directories
addon_id = driver.install_addon("extensions/my-addon.xpi", temporary=True)
driver.get("https://app.example.com")
# ... assertions ...
driver.uninstall_addon(addon_id)
const firefox = require('selenium-webdriver/firefox');
const driver = await new Builder().forBrowser('firefox').build();
// second argument: install as temporary (unsigned allowed)
const id = await driver.installAddon('extensions/my-addon.xpi', true);
await driver.get('https://app.example.com');
// ... assertions ...
await driver.uninstallAddon(id);
var driver = new FirefoxDriver();
// second argument: install as temporary (unsigned allowed)
var id = driver.InstallAddOnFromFile("extensions/my-addon.xpi", true);
driver.Navigate().GoToUrl("https://app.example.com");
// ... assertions ...
driver.UninstallAddOn(id);

In Python 4.52 and JavaScript 4.51+, install_addon/installAddon still work but are deprecated in favour of driver.install_web_extension(path) / driver.installWebExtension(path), which return an extension object you pass to uninstall_web_extension / uninstallWebExtension. Those methods work across browsers (Chromium needs BiDi enabled) and fall back to the classic add-on endpoint on Firefox.

Chrome and Edge: load at startup

Add a packed .crx extension through options
Selenium 4 Medium
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("extensions/my-extension.crx"));
WebDriver driver = new ChromeDriver(options);
options = webdriver.ChromeOptions()
options.add_extension("extensions/my-extension.crx")
driver = webdriver.Chrome(options=options)
const chrome = require('selenium-webdriver/chrome');
const options = new chrome.Options().addExtensions('extensions/my-extension.crx');
const driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
var options = new ChromeOptions();
options.AddExtension("extensions/my-extension.crx");
var driver = new ChromeDriver(options);

Finding and Opening the Extension

Firefox returns the add-on ID from the install call. Chrome assigns a deterministic ID derived from the extension’s key; read it once from chrome://extensions (Developer mode) and keep it in config, or set a fixed key in manifest.json during development. Extension pages are then reachable directly, which is much easier than clicking the toolbar icon (which WebDriver cannot see):

chrome-extension://<extension-id>/popup.html
moz-extension://<internal-uuid>/popup.html

Firefox’s moz-extension:// host is a per-profile UUID, not the add-on ID; read it from about:debugging#/runtime/this-firefox or have the extension expose it in a test build.

WebDriver BiDi

BiDi’s webExtension.install and webExtension.uninstall commands do the same job through one cross-browser API, taking a path, an archive, or base64 data. Recent releases added driver-level methods that use it: Ruby in 4.50, JavaScript in 4.51 (installWebExtension) and Python in 4.52 (install_web_extension); Java exposes the @Beta org.openqa.selenium.bidi.webextension.WebExtension class. Chrome only allows BiDi extension installs when started with flags that enable extension debugging, so check the current Selenium docs for your browser before switching.

Gotchas

  • Branded Chrome restricts command-line extension loading. Branded stable Chrome ignores the --load-extension switch since Chrome 137. Use Chrome for Testing (which Selenium Manager downloads when you request a specific browser version) or Chromium for extension work.
  • Headless mode. The new headless mode supports extensions; the old one did not. If an extension never loads, confirm you are not on a very old Chrome.
  • Firefox signing. Release Firefox only installs signed add-ons permanently. Temporary installs bypass that for the life of the session, which is usually what tests want.
  • Extensions add flakiness. They run their own scripts on every page. Use a dedicated driver configuration for extension tests and keep the rest of the suite extension-free.

Learn the theory