SeleniumHQ/selenium · warning · KeyError

No script with key: {script_key} existed in {self.pinned_scr

Error message

No script with key: {script_key} existed in {self.pinned_scripts}

What it means

Raised by the deprecated `unpin` method when `self.pinned_scripts.pop(script_key.id)` raises KeyError — i.e. the given ScriptKey.id is not in the pinned_scripts registry. The method then re-raises a KeyError with a descriptive message. Note `unpin` is deprecated in favor of `driver.script.unpin()`.

Source

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

    def unpin(self, script_key: ScriptKey) -> None:
        """Remove a pinned script from storage.

        .. deprecated::
            Use ``driver.script.unpin()`` instead, which uses the WebDriver BiDi protocol.

        Example:
            `driver.unpin(script_key)`
        """
        warnings.warn(
            "unpin is deprecated, use driver.script.unpin() instead",
            DeprecationWarning,
            stacklevel=2,
        )
        try:
            self.pinned_scripts.pop(script_key.id)
        except KeyError:
            raise KeyError(f"No script with key: {script_key} existed in {self.pinned_scripts}") from None

    def get_pinned_scripts(self) -> list[str]:
        """Return a list of all pinned scripts.

        .. deprecated::
            Use ``driver.script.pin()`` to manage preload scripts via the WebDriver BiDi protocol.

        Example:
            `pinned_scripts = driver.get_pinned_scripts()`
        """
        warnings.warn(
            "get_pinned_scripts is deprecated, use driver.script.pin() to manage preload scripts instead",
            DeprecationWarning,
            stacklevel=2,
        )
        return list(self.pinned_scripts)

    def execute_script(self, script: str, *args) -> Any:

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Migrate to driver.script.unpin() (the non-deprecated BiDi-based API).
  2. Only unpin keys returned by a successful pin(); track them in a set.
  3. Guard with `if script_key.id in driver.pinned_scripts` before unpinning.

Example fix

# before
driver.unpin(key)  # KeyError if key.id not pinned

# after
if key.id in driver.pinned_scripts:
    driver.unpin(key)  # or driver.script.unpin(key)
Defensive patterns

Strategy: validation

Validate before calling

if script_key.id not in driver.pinned_scripts:
    raise KeyError(f'{script_key.id} is not pinned')
driver.unpin(script_key)

Type guard

def is_pinned(driver, script_key) -> bool:
    return script_key.id in driver.pinned_scripts

Try / catch

try:
    driver.unpin(key)
except KeyError:
    # already unpinned or never pinned; nothing to do

Prevention

When it happens

Trigger: Calling driver.unpin(script_key) with a ScriptKey that was never pinned, was already unpinned, or whose id doesn't match an entry in pinned_scripts.

Common situations: Unpinning a script twice; holding stale ScriptKey references across a session restart; mixed usage of the old unpin() and the new script API.

Related errors


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