Implementation:SeleniumHQ Selenium ScriptKey
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Browser_Automation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for representing a unique identifier for a JavaScript script within Selenium's script pinning mechanism, provided by the Selenium WebDriver library.
Description
ScriptKey is a Java class in the org.openqa.selenium package that serves as an immutable identifier for a JavaScript script. It wraps a non-null String identifier and provides proper value-object semantics through equals() and hashCode() implementations based on the identifier value.
The class acts as the base type for script identification in Selenium's pinned-script infrastructure. It supports JSON serialization through a private toJson() method that returns the raw identifier string, and deserialization through a private static fromJson(String) factory method.
Key characteristics:
- Immutable -- the identifier is set at construction time and cannot be changed.
- Value semantics -- two
ScriptKeyinstances with the same identifier are considered equal. - Non-null enforcement -- the constructor uses
Require.nonNullto reject null identifiers.
Usage
Use ScriptKey as a handle to track and reference pinned scripts within the WebDriver session. It is typically not used directly by end users but rather as part of the script-pinning API:
- Created when a script is pinned to a WebDriver session.
- Used to reference or execute the pinned script later.
- Extended by
UnpinnedScriptKeyfor the full pinned-script lifecycle.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/ScriptKey.java
Signature
public class ScriptKey {
public ScriptKey(String identifier)
public String getIdentifier()
public boolean equals(Object o)
public int hashCode()
}
Import
import org.openqa.selenium.ScriptKey;
I/O Contract
| Parameter | Type | Constraints | Description |
|---|---|---|---|
identifier |
String |
Non-null (enforced) | Unique string identifier for the script |
| Method | Input | Output | Description |
|---|---|---|---|
getIdentifier() |
none | String |
Returns the script's unique identifier |
equals(Object o) |
Object (nullable) |
boolean |
Returns true if the other object is a ScriptKey with the same identifier |
hashCode() |
none | int |
Returns a hash code based on the identifier |
| Method | Direction | Type | Description |
|---|---|---|---|
toJson() |
Serialize | String |
Returns the raw identifier string for JSON output |
fromJson(String) |
Deserialize | ScriptKey |
Constructs a ScriptKey from a JSON identifier string |
Usage Examples
// ScriptKey is typically created internally by the pinning mechanism
// Direct usage:
ScriptKey key = new ScriptKey("my-script-id-12345");
String id = key.getIdentifier(); // "my-script-id-12345"
// Value equality
ScriptKey key1 = new ScriptKey("abc");
ScriptKey key2 = new ScriptKey("abc");
boolean areEqual = key1.equals(key2); // true
// Use in collections (proper hashCode support)
Set<ScriptKey> scriptKeys = new HashSet<>();
scriptKeys.add(new ScriptKey("script-1"));
scriptKeys.add(new ScriptKey("script-2"));
Related Pages
- Implementation:SeleniumHQ_Selenium_UnpinnedScriptKey -- extends ScriptKey with full pinned-script lifecycle management
- Implementation:SeleniumHQ_Selenium_WebDriverException -- base exception for WebDriver errors
- Concept:JavaScript_Execution -- the JavaScript execution model where ScriptKey is used