{"record":{"id":"fe6de701280d4abd","repo":"SeleniumHQ/selenium","slug":"pinned-script-could-not-be-found","errorCode":null,"errorMessage":"Pinned script could not be found","messagePattern":"Pinned script could not be found","errorType":"exception","errorClass":"JavascriptException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/remote/webdriver.py","lineNumber":608,"sourceCode":"    def execute_script(self, script: str, *args) -> Any:\n        \"\"\"Synchronously Executes JavaScript in the current window/frame.\n\n        Args:\n            script: The javascript to execute.\n            *args: Any applicable arguments for your JavaScript.\n\n        Example:\n            ```\n            id = \"username\"\n            value = \"test_user\"\n            driver.execute_script(\"document.getElementById(arguments[0]).value = arguments[1];\", id, value)\n            ```\n        \"\"\"\n        if isinstance(script, ScriptKey):\n            try:\n                script = self.pinned_scripts[script.id]\n            except KeyError:\n                raise JavascriptException(\"Pinned script could not be found\")\n\n        converted_args = list(args)\n        command = Command.W3C_EXECUTE_SCRIPT\n\n        return self.execute(command, {\"script\": script, \"args\": converted_args})[\"value\"]\n\n    def execute_async_script(self, script: str, *args) -> Any:\n        \"\"\"Asynchronously Executes JavaScript in the current window/frame.\n\n        Args:\n            script: The javascript to execute.\n            *args: Any applicable arguments for your JavaScript.\n\n        Example:\n            ```\n            script = \"var callback = arguments[arguments.length - 1]; \"\n                \"window.setTimeout(function(){ callback('timeout') }, 3000);\"\n            driver.execute_async_script(script)","sourceCodeStart":590,"sourceCodeEnd":626,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/remote/webdriver.py#L590-L626","documentation":"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.","triggerScenarios":"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.","commonSituations":"Unpinning a script then still executing it; reusing ScriptKey objects after a driver restart; session not restoring pinned scripts (BiDi vs legacy mismatch).","solutions":["Re-pin the script with driver.script.pin() (or pin()) before executing via its key.","Confirm `script_key.id in driver.pinned_scripts` before execute_script.","For one-off scripts, pass the JS source string directly instead of a ScriptKey."],"exampleFix":"# before\ndriver.execute_script(key, *args)  # key not in pinned_scripts\n\n# after\nif key.id not in driver.pinned_scripts:\n    key = driver.script.pin(js_source)\ndriver.execute_script(key, *args)","handlingStrategy":"validation","validationCode":"from selenium.webdriver.common.script_key import ScriptKey\nif isinstance(script, ScriptKey) and script.id not in driver.pinned_scripts:\n    raise JavascriptException('script key not pinned; pin it first')","typeGuard":"def script_is_pinned(driver, script) -> bool:\n    from selenium.webdriver.common.script_key import ScriptKey\n    return not isinstance(script, ScriptKey) or script.id in driver.pinned_scripts","tryCatchPattern":"try:\n    driver.execute_script(key, *args)\nexcept JavascriptException:\n    key = driver.script.pin(js_source)\n    driver.execute_script(key, *args)","preventionTips":["Re-pin after restarts; ScriptKey objects don't survive a new session.","Don't mix pinned-key execution with one-off string scripts without a membership check."],"tags":["scripts","validation","webdriver"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}