Implementation:SeleniumHQ Selenium FirefoxCommandContext
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Firefox_Browser |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
FirefoxCommandContext is an enum representing the two valid command execution contexts in Firefox: content (web page) and chrome (browser UI).
Description
The FirefoxCommandContext enum defines the two execution contexts available in Firefox via the Marionette protocol. The CONTENT context targets the web page loaded in the browser tab, which is the default context for most WebDriver operations. The CHROME context targets the browser's own UI elements (the "chrome" layer), allowing interaction with browser-level components such as the address bar, toolbar, or internal about: pages. Each enum constant stores a lowercase string representation ("content" or "chrome") and provides a fromString() factory method for case-insensitive parsing.
Usage
Use FirefoxCommandContext in conjunction with the HasContext interface to switch between the web content context and the browser chrome context. This is useful when you need to interact with Firefox's internal UI elements or execute privileged JavaScript in the chrome context.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/firefox/FirefoxCommandContext.java
Signature
public enum FirefoxCommandContext {
CONTENT("content"),
CHROME("chrome");
FirefoxCommandContext(String text)
@Override
public String toString()
public static FirefoxCommandContext fromString(String text)
}
Import
import org.openqa.selenium.firefox.FirefoxCommandContext;
I/O Contract
Enum Constants
| Constant | String Value | Description |
|---|---|---|
CONTENT |
"content" |
Commands operate on the web page loaded in the browser tab. |
CHROME |
"chrome" |
Commands operate on the browser's own UI elements (chrome layer). |
Methods
| Method | Input | Output | Description |
|---|---|---|---|
toString |
none | String |
Returns the lowercase string representation of the context ("content" or "chrome").
|
fromString |
String text |
FirefoxCommandContext (nullable) |
Parses a string to the matching enum constant (case-insensitive). Returns null if no match is found or input is null.
|
Usage Examples
// Switch to chrome context to interact with browser UI
FirefoxDriver driver = new FirefoxDriver();
HasContext contextDriver = (HasContext) driver;
// Switch to the browser chrome context
contextDriver.setContext(FirefoxCommandContext.CHROME);
// Execute JavaScript in the chrome context
((JavascriptExecutor) driver).executeScript("/* chrome-level JS */");
// Switch back to content context
contextDriver.setContext(FirefoxCommandContext.CONTENT);
// Parse a context string
FirefoxCommandContext context = FirefoxCommandContext.fromString("chrome");
// context == FirefoxCommandContext.CHROME
FirefoxCommandContext unknown = FirefoxCommandContext.fromString("invalid");
// unknown == null
Related Pages
- HasContext - Interface that uses
FirefoxCommandContextfor context switching - FirefoxDriver - Implements
HasContextand supports context switching - FirefoxOptions - Configuration options for Firefox sessions
- GeckoDriverService - Manages the geckodriver process that supports context switching