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
geckodriveron 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])
Explanation
Lines 1–2: We import the
tempfilemodule for creating temporary files and directories and thewebdrivermodule from the Selenium library for browser automation.Line 5: We set the path to the
geckodriverexecutable, which is necessary for Selenium to interact with the Firefox browser.Line 8: We used the
tempfile.mkdtempfunction to create a temporary directory with aselenium_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 thecustom_profile_pathpath.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
.xpifile 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.
Unlock your potential: Selenium WebDriver series, all in one place!
To continue your exploration of Selenium WebDriver, check out our series of Answers below:
How to generate an XSLT report in Selenium web driver
Learn how to execute Selenium WebDriver tests, capture results, convert them to XML, transform with XSLT, and display the HTML report.What are the element locators in Selenium WebDriver?
Learn how Selenium WebDriver uses locators like ID, name, class, CSS, and XPath to pinpoint and interact with web elements precisely.How to create a Firefox profile in Selenium WebDriver
Learn how to create and customize Firefox profiles in Selenium WebDriver for tailored automated testing, adding extensions and themes to simulate various user scenarios.What is Robot Class in selenium webdriver and why it is used?
Learn how to enhance Selenium WebDriver by simulating native OS interactions, handling system dialogs, and executing complex actions beyond WebDriver's capabilities.
Free Resources