Firefox specific functionality
Selenium 4 requires Firefox 78 or greater. It is recommended to always use the latest version of geckodriver.
Options
Capabilities common to all browsers are described on the Options page.
Capabilities unique to Firefox can be found at Mozilla’s page for firefoxOptions
Starting a Firefox session with basic defined options looks like this:
FirefoxOptions options = new FirefoxOptions();
driver = new FirefoxDriver(options);
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(options=options)
var options = new FirefoxOptions();
driver = new FirefoxDriver(options);
options = Selenium::WebDriver::Options.firefox
@driver = Selenium::WebDriver.for :firefox, options: options
driver = new Builder()
.forBrowser(Browser.FIREFOX)
.setFirefoxOptions(options)
.build();
Arguments
The args
parameter is for a list of Command line switches used when starting the browser.
Commonly used args include -headless
and "-profile", "/path/to/profile"
Add an argument to options:
options.addArguments("-headless");
options.add_argument("-headless")
options.AddArgument("-headless");
options.args << '-headless'
.setFirefoxOptions(options.addArguments('--headless'))
Start browser in a specified location
The binary
parameter takes the path of an alternate location of browser to use. For example, with this parameter you can
use geckodriver to drive Firefox Nightly instead of the production version when both are present on your computer.
Add a browser location to options:
options.setBinary(getFirefoxLocation());
options.binary_location = firefox_bin
options.BinaryLocation = GetFirefoxLocation();
options.binary = firefox_location
Profiles
There are several ways to work with Firefox profiles.
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setPreference("javascript.enabled", "False");
options.setProfile(profile);
driver = new FirefoxDriver(options);
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
options = Options()
firefox_profile = FirefoxProfile()
firefox_profile.set_preference("javascript.enabled", False)
options.profile = firefox_profile
driver = webdriver.Firefox(options=options)
var options = new FirefoxOptions();
var profile = new FirefoxProfile();
options.Profile = profile;
var driver = new FirefoxDriver(options);
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = '/tmp/webdriver-downloads'
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
const { Builder } = require("selenium-webdriver");
const firefox = require('selenium-webdriver/firefox');
const options = new firefox.Options();
let profile = '/path to custom profile';
options.setProfile(profile);
const driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
val options = FirefoxOptions()
options.profile = FirefoxProfile()
driver = FirefoxDriver(options)
Service
Service settings common to all browsers are described on the Service page.
Log output
Getting driver logs can be helpful for debugging various issues. The Service class lets you direct where the logs will go. Logging output is ignored unless the user directs it somewhere.
File output
To change the logging output to save to a specific file:
FirefoxDriverService service =
new GeckoDriverService.Builder().withLogFile(logLocation).build();
Note: Java also allows setting file output by System Property:
Property key: GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY
Property value: String representing path to log file
service = webdriver.FirefoxService(log_output=log_path, service_args=['--log', 'debug'])
Console output
To change the logging output to display in the console:
FirefoxDriverService service =
new GeckoDriverService.Builder().withLogOutput(System.out).build();
Note: Java also allows setting console output by System Property;
Property key: GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY
Property value: DriverService.LOG_STDOUT
or DriverService.LOG_STDERR
service = webdriver.FirefoxService(log_output=subprocess.STDOUT)
Log level
There are 7 available log levels: fatal
, error
, warn
, info
, config
, debug
, trace
.
If logging is specified the level defaults to info
.
Note that -v
is equivalent to -log debug
and -vv
is equivalent to log trace
,
so this examples is just for setting the log level generically:
FirefoxDriverService service =
new GeckoDriverService.Builder().withLogLevel(FirefoxDriverLogLevel.DEBUG).build();
Note: Java also allows setting log level by System Property:
Property key: GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY
Property value: String representation of FirefoxDriverLogLevel
enum
service = webdriver.FirefoxService(log_output=log_path, service_args=['--log', 'debug'])
Truncated Logs
The driver logs everything that gets sent to it, including string representations of large binaries, so Firefox truncates lines by default. To turn off truncation:
FirefoxDriverService service =
new GeckoDriverService.Builder().withTruncatedLogs(false).build();
Note: Java also allows setting log level by System Property:
Property key: GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE
Property value: "true"
or "false"
service = webdriver.FirefoxService(service_args=['--log-no-truncate', '--log', 'debug'], log_output=log_path)
Profile Root
The default directory for profiles is the system temporary directory. If you do not have access to that directory, or want profiles to be created some place specific, you can change the profile root directory:
FirefoxDriverService service =
new GeckoDriverService.Builder().withProfileRoot(profileDirectory).build();
Note: Java also allows setting log level by System Property:
Property key: GeckoDriverService.GECKO_DRIVER_PROFILE_ROOT
Property value: String representing path to profile root directory
service = webdriver.FirefoxService(service_args=['--profile-root', temp_dir])
Special Features
Some browsers have implemented additional features that are unique to them.
Add-ons
Unlike Chrome, Firefox extensions are not added as part of capabilities as mentioned in this issue, they are created after starting the driver.
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
Installation
A signed xpi file you would get from Mozilla Addon page
driver.installExtension(xpiPath);
driver.install_addon(addon_path_xpi)
driver.InstallAddOnFromFile(Path.GetFullPath(extensionFilePath));
driver.install_addon(extension_file_path)
let id = await driver.installAddon(xpiPath);
Uninstallation
Uninstalling an addon requires knowing its id. The id can be obtained from the return value when installing the add-on.
driver.uninstallExtension(id);
driver.uninstall_addon(id)
driver.uninstall_addon(extension_id)
await driver.uninstallAddon(id);
Unsigned installation
When working with an unfinished or unpublished extension, it will likely not be signed. As such, it can only be installed as “temporary.” This can be done by passing in either a zip file or a directory, here’s an example with a directory:
driver.installExtension(path, true);
driver.install_addon(addon_path_dir, temporary=True)
driver.InstallAddOnFromDirectory(Path.GetFullPath(extensionDirPath), true);
let id = await driver.installAddon(xpiPath, true);
Full page screenshots
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE);
driver.save_full_page_screenshot("full_page_screenshot.png")
screenshot = driver.save_full_page_screenshot(File.join(dir, 'screenshot.png'))
Context
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
((HasContext) driver).setContext(FirefoxCommandContext.CHROME);
driver.executeScript("console.log('Inside Chrome context');");
with driver.context(driver.CONTEXT_CHROME):
driver.execute_script("console.log('Inside Chrome context');")
driver.context = 'content'