SeleniumHQ/selenium · error · JavascriptException

Pinned script could not be found

Error message

Pinned script could not be found

What it means

Raised by `execute_script` when its `script` argument is a `ScriptKey` whose id is absent from `pinned_scripts`. The method tries to resolve `self.pinned_scripts[script.id]`; on KeyError it raises a JavascriptException because the failure manifests at script-execution time. It indicates the pinned preload script is no longer registered.

Source

Thrown at py/selenium/webdriver/remote/webdriver.py:608

    def execute_script(self, script: str, *args) -> Any:
        """Synchronously Executes JavaScript in the current window/frame.

        Args:
            script: The javascript to execute.
            *args: Any applicable arguments for your JavaScript.

        Example:
            ```
            id = "username"
            value = "test_user"
            driver.execute_script("document.getElementById(arguments[0]).value = arguments[1];", id, value)
            ```
        """
        if isinstance(script, ScriptKey):
            try:
                script = self.pinned_scripts[script.id]
            except KeyError:
                raise JavascriptException("Pinned script could not be found")

        converted_args = list(args)
        command = Command.W3C_EXECUTE_SCRIPT

        return self.execute(command, {"script": script, "args": converted_args})["value"]

    def execute_async_script(self, script: str, *args) -> Any:
        """Asynchronously Executes JavaScript in the current window/frame.

        Args:
            script: The javascript to execute.
            *args: Any applicable arguments for your JavaScript.

        Example:
            ```
            script = "var callback = arguments[arguments.length - 1]; "
                "window.setTimeout(function(){ callback('timeout') }, 3000);"
            driver.execute_async_script(script)

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Re-pin the script with driver.script.pin() (or pin()) before executing via its key.
  2. Confirm `script_key.id in driver.pinned_scripts` before execute_script.
  3. For one-off scripts, pass the JS source string directly instead of a ScriptKey.

Example fix

# before
driver.execute_script(key, *args)  # key not in pinned_scripts

# after
if key.id not in driver.pinned_scripts:
    key = driver.script.pin(js_source)
driver.execute_script(key, *args)
Defensive patterns

Strategy: validation

Validate before calling

from selenium.webdriver.common.script_key import ScriptKey
if isinstance(script, ScriptKey) and script.id not in driver.pinned_scripts:
    raise JavascriptException('script key not pinned; pin it first')

Type guard

def script_is_pinned(driver, script) -> bool:
    from selenium.webdriver.common.script_key import ScriptKey
    return not isinstance(script, ScriptKey) or script.id in driver.pinned_scripts

Try / catch

try:
    driver.execute_script(key, *args)
except JavascriptException:
    key = driver.script.pin(js_source)
    driver.execute_script(key, *args)

Prevention

When it happens

Trigger: Calling driver.execute_script(script_key, ...) where script_key was unpinned, evicted, or belongs to a different/old session whose pinned_scripts weren't restored.

Common situations: Unpinning a script then still executing it; reusing ScriptKey objects after a driver restart; session not restoring pinned scripts (BiDi vs legacy mismatch).

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/fe6de701280d4abd. Report an issue: GitHub.