How to create a Firefox profile in Selenium WebDriver

Selenium is widely used for automated testing and has emerged as a powerful tool for web browser automation. One of the key aspects of Selenium is the ability to create and customize browser profiles, allowing testers to simulate various user scenarios. In this Answer, we’ll focus on creating a Firefox profile and adding extensions and themes using Selenium.

What is a profile?

A Firefox profile contains settings, preferences, and configurations defining the browser’s behavior. We can control various browser aspects, such as cookies, extensions, and browser history.

Dependencies

We need to install the following dependencies to work with Selenium and Firefox. To work with Selenium, we need Python and pip package manager. We can install Selenium using the command below:

pip3 install selenium

We need to install geckodriver for Firefox WebDriver. This has the same functionality as the Chrome WebDriver. To install it on Linux, we need to run the following command:

apt-get install firefox-geckodriver

Note: Learn how to install geckodriver on other platform using the official documentation.

Let’s understand how to create and customize a Firefox profile using Selenium by example.

Example

In the playground below, we create a new profile and add darkreader-4.9.70.xpi extension. We also set the dark theme. To see this in action, press the “Run” button;

import tempfile
from selenium import webdriver

# Set the path to the GeckoDriver executable
custom_gecko_path = "/usr/bin/geckodriver"

# Set the path using the executable_path argument in FirefoxOptions
custom_profile_path = tempfile.mkdtemp(prefix="selenium_custom_profile_")

# Create a new Firefox profile
custom_firefox_options = webdriver.FirefoxOptions()
custom_firefox_options.profile = webdriver.FirefoxProfile(custom_profile_path)
custom_firefox_options.add_argument(f"webdriver.gecko.driver={custom_gecko_path}")

# Set the theme to dark mode
custom_firefox_options.set_preference("ui.systemUsesDarkTheme", 1)

# Initialize the Firefox WebDriver
custom_driver = webdriver.Firefox(options=custom_firefox_options)

# Get the extension file path
custom_extension_path = "/darkreader-4.9.70.xpi"
# Install the extension
custom_driver.install_addon(custom_extension_path, temporary=True)

# Now you can use the driver as usual
custom_driver.get("https://google.com")

custom_driver.switch_to.window(custom_driver.window_handles[0])
Create a new Firefox profile using Selenium

Explanation

  • Lines 1–2: We import the tempfile module for creating temporary files and directories and the webdriver module from the Selenium library for browser automation.

  • Line 5: We set the path to the geckodriver executable, which is necessary for Selenium to interact with the Firefox browser.

  • Line 8: We used the tempfile.mkdtemp function to create a temporary directory with a selenium_custom_profile_ prefix. This directory will be used as the profile directory for the Firefox browser.

  • Lines 11–13: We created a new instance of FirefoxOptions, and configured the options. The Firefox profile is set to the custom_profile_path path.

  • Line 16: We set the dark theme.

  • Line 19: We initialize a new instance of Firefox using the configured options.

  • Line 22, 24: We installed a dark reader extension. We need to download the .xpi file manually and provide the path to it.

  • Line 27: Finally, we open the Google home page.
    Line 29 (optional): We switch to the main tab of the browser.

Conclusion

We have successfully created a custom Firefox profile using Selenium. This capability opens up possibilities for tailoring our automated tests to specific user scenarios. Experiment with different profile preferences to fine-tune your testing environment and ensure your web applications function seamlessly across various user configurations.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved