Implementation:SeleniumHQ Selenium HasContext
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Firefox_Browser |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
HasContext is a Beta interface that allows classes to switch the Firefox command execution context between web content and browser chrome.
Description
The HasContext interface, annotated with @Beta, declares two methods for managing the Firefox Marionette command context. The setContext() method changes whether subsequent commands operate on the web page content loaded in the browser tab or on the browser's own chrome UI elements. The getContext() method retrieves the current active context. This interface leverages the FirefoxCommandContext enum to represent the two valid contexts: CONTENT (default, targeting the web page) and CHROME (targeting browser-level UI and privileged JavaScript execution). This capability is specific to Firefox and is not part of the standard W3C WebDriver specification.
Usage
Use HasContext when you need to execute commands or JavaScript in the Firefox browser chrome context, such as accessing browser internals, modifying about:config preferences at runtime, or interacting with browser toolbar elements. Cast your FirefoxDriver instance to HasContext to access context-switching methods.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/firefox/HasContext.java
Signature
@Beta
public interface HasContext {
void setContext(FirefoxCommandContext context);
FirefoxCommandContext getContext();
}
Import
import org.openqa.selenium.firefox.HasContext;
I/O Contract
Methods
| Method | Input | Output | Description |
|---|---|---|---|
setContext |
FirefoxCommandContext context |
void |
Sets the command context to either CONTENT (page loaded in browser) or CHROME (browser UI elements hosting the page).
|
getContext |
none | FirefoxCommandContext |
Returns the current command context that subsequent operations will target. |
Context Values
| Context | Description |
|---|---|
FirefoxCommandContext.CONTENT |
Commands operate on the web page in the browser tab (default). |
FirefoxCommandContext.CHROME |
Commands operate on the browser's own UI layer, enabling privileged access. |
Usage Examples
// Switch to chrome context and execute privileged JavaScript
FirefoxDriver driver = new FirefoxDriver();
HasContext contextDriver = (HasContext) driver;
// Switch to the browser chrome context
contextDriver.setContext(FirefoxCommandContext.CHROME);
// Execute JavaScript with chrome privileges
Object result = ((JavascriptExecutor) driver).executeScript(
"return Services.prefs.getCharPref('browser.startup.homepage');");
// Switch back to content context for normal page interaction
contextDriver.setContext(FirefoxCommandContext.CONTENT);
driver.get("https://example.com");
// Query the current context
HasContext contextDriver = (HasContext) driver;
FirefoxCommandContext currentContext = contextDriver.getContext();
System.out.println("Current context: " + currentContext);
// Output: Current context: content
Related Pages
- FirefoxCommandContext - Enum defining the
CONTENTandCHROMEcontext values - FirefoxDriver - Implements
HasContextto provide context switching - HasExtensions - Another Firefox-specific capability interface
- HasFullPageScreenshot - Another Firefox-specific capability interface
- GeckoDriverService - Manages the geckodriver process that supports context switching via Marionette