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:
Assertions.assertEquals("Content injected by webextensions-selenium-example", injected.getText());
}
driver = webdriver.Firefox(options=options)
driver.InstallAddOnFromFile(Path.GetFullPath(extensionFilePath));
@driver = Selenium::WebDriver.for :firefox, options: options
end
let options = new firefox.Options();
driver = await env.builder()
.setFirefoxOptions(options)
.build();
Here are a few common use cases with different capabilities:
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:
driver.uninstallExtension(id);
public void UnInstallAddon()
driver.get 'https://www.selenium.dev/selenium/web/blank.html'
let driver = await env.builder()
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:
driver.installExtension(path, true);
Assert.AreEqual(driver.FindElements(By.Id("webextensions-selenium-example")).Count, 0);
driver.uninstall_addon(extension_id)
Profiles
There are several ways to work with Firefox profiles
var options = new FirefoxOptions();
var profile = new FirefoxProfile();
options.Profile = profile;
var driver = new RemoteWebDriver(options);
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 = RemoteWebDriver(options)
Note: Whether you create an empty FirefoxProfile
or point it to the directory of your own profile, Selenium
will create a temporary directory to store either the data of the new profile or a copy of your existing one. Every
time you run your program, a different temporary directory will be created. These directories are not cleaned up
explicitly by Selenium, they should eventually get removed by the operating system. However, if you want to remove
the copy manually (e.g. if your profile is large in size), the path of the copy is exposed by the FirefoxProfile
object. Check the language specific implementation to see how to retrieve that location.
If you want to use an existing Firefox profile, you can pass in the path to that profile. Please refer to the official Firefox documentation for instructions on how to find the directory of your profile.
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:
System.setOut(new PrintStream(getLogLocation()));
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
@driver = Selenium::WebDriver.for :firefox, service: service
Console output
To change the logging output to display in the console:
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY,
getLogLocation().getAbsolutePath());
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
@driver = Selenium::WebDriver.for :firefox, service: service
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:
public void stopsTruncatingLogs() throws IOException {
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY,
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
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:
@Test
public void setProfileLocation() throws IOException {
Note: Java also allows setting log level by System Property:
Property key: GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE
Property value: "true"
or "false"
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:
@Test
public void installAddon() {
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
Special Features
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
await driver.uninstallAddon(id);
Uninstallation
Uninstalling an addon requires knowing its id. The id can be obtained from the return value when installing the add-on.
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:
Full page screenshots
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
Context
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
Note: As of Firefox 138, geckodriver needs to be started with the argument --allow-system-access
to switch the context to CHROME
.