Skip to content

Latest commit

 

History

History
457 lines (397 loc) · 15.2 KB

File metadata and controls

457 lines (397 loc) · 15.2 KB
title Firefox specific functionality
linkTitle Firefox
weight 6
description These are capabilities and features specific to Mozilla Firefox browsers.
aliases
/zh-cn/documentation/capabilities/firefox

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]({{< ref "../drivers/options.md" >}}).

Capabilities unique to Firefox can be found at Mozilla's page for firefoxOptions

Starting a Firefox session with basic defined options looks like this:

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L36-L37" >}} {{< /tab >}} {{% tab header="Python" %}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L10-L11" >}} {{% /tab %}} {{< tab header="CSharp" >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs#L34-L35" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L10-L11" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="/examples/javascript/test/getting_started/openFirefoxTest.spec.js#L10-L13">}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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:

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L44" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L19" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs#L43" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L17" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="/examples/javascript/test/browser/firefoxSpecificFunctionalities.spec.js#L12">}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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:

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L54" >}} {{< /tab >}} {{% tab header="Python" %}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L28" >}} {{% /tab %}} {{< tab header="CSharp" >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs#L53" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L25" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

Profiles

There are several ways to work with Firefox profiles

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L211-L216" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L157-L165" >}} {{< /tab >}} {{< tab header="CSharp" >}} var options = new FirefoxOptions(); var profile = new FirefoxProfile(); options.Profile = profile; var driver = new RemoteWebDriver(options); {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L139-L141" >}} {{< /tab >}} {{< tab header="JavaScript" >}} 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(); {{< /tab >}} {{< tab header="Kotlin" >}} val options = FirefoxOptions() options.profile = FirefoxProfile() driver = RemoteWebDriver(options) {{< /tab >}} {{< /tabpane >}}

Service

Service settings common to all browsers are described on the [Service page]({{< ref "../drivers/service.md" >}}).

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:

{{< tabpane text=true >}} {{% tab header="Java" %}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L62-L63" >}} 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 {{% /tab %}} {{< tab header="Python" >}} {{< badge-version version="4.11" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L36" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-implementation >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/ruby/spec/browsers/firefox_spec.rb#L43" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

Console output

To change the logging output to display in the console:

{{< tabpane text=true >}} {{% tab header="Java" %}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L76-L77" >}} 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 {{% /tab %}} {{< tab header="Python" >}} {{< badge-version version="4.11" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L48" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-implementation >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/ruby/spec/browsers/firefox_spec.rb#L52" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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:

{{< tabpane text=true >}} {{% tab header="Java" %}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L90-L91" >}} 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 {{% /tab %}} {{< tab header="Python" >}} {{< badge-version version="4.11" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L59" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-implementation >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/ruby/spec/browsers/firefox_spec.rb#L63" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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:

{{< tabpane text=true >}} {{% tab header="Java" %}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L106-L107" >}} Note: Java also allows setting log level by System Property:
Property key: GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE
Property value: "true" or "false" {{% /tab %}} {{< tab header="Python" >}} {{< badge-version version="4.11" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L70" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-implementation >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/ruby/spec/browsers/firefox_spec.rb#L72" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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:

{{< tabpane text=true >}} {{% tab header="Java" %}} {{< badge-version version="4.10" >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L118-L119" >}} 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 {{% /tab %}} {{< tab header="Python" >}} {{< gh-codeblock path="examples/python/tests/browsers/test_firefox.py#L81" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-implementation >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-version version="4.8" >}} {{< gh-codeblock path="examples/ruby/spec/browsers/firefox_spec.rb#L81" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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]({{< ref "../drivers/remote_webdriver" >}}) page.

Installation

A signed xpi file you would get from Mozilla Addon page

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L133" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L94" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs#L137" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L95" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="/examples/javascript/test/browser/firefoxSpecificFunctionalities.spec.js#L25">}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

Uninstallation

Uninstalling an addon requires knowing its id. The id can be obtained from the return value when installing the add-on.

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L148" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L106" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-version version="4.5" >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs#L152" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L106" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="/examples/javascript/test/browser/firefoxSpecificFunctionalities.spec.js#L26">}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

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:

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L160" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L115" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-version version="4.5" >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs#L165" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-version version="4.5" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L115" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="/examples/javascript/test/browser/firefoxSpecificFunctionalities.spec.js#L41">}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

Full page screenshots

The following examples are for local webdrivers. For remote webdrivers, please refer to the [Remote WebDriver]({{< ref "../drivers/remote_webdriver" >}}) page.

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L181" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L139" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L125" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

Context

The following examples are for local webdrivers. For remote webdrivers, please refer to the [Remote WebDriver]({{< ref "../drivers/remote_webdriver" >}}) page.

{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java#L197-L198" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_firefox.py#L149-L150" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="/examples/ruby/spec/browsers/firefox_spec.rb#L132" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< badge-code >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}

Note: As of Firefox 138, geckodriver needs to be started with the argument --allow-system-access to switch the context to CHROME.