{"record":{"id":"d0521fa710001671","repo":"browser-use/browser-use","slug":"javascript-code-is-empty-after-cleaning","errorCode":null,"errorMessage":"JavaScript code is empty after cleaning","messagePattern":"JavaScript code is empty after cleaning","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"browser_use/actor/page.py","lineNumber":188,"sourceCode":"\t\tif (js_code.startswith('\"') and js_code.endswith('\"')) or (js_code.startswith(\"'\") and js_code.endswith(\"'\")):\n\t\t\t# Check if it's a wrapped string (not part of JS syntax)\n\t\t\tinner = js_code[1:-1]\n\t\t\tif inner.count('\"') + inner.count(\"'\") == 0 or '() =>' in inner:\n\t\t\t\tjs_code = inner\n\n\t\t# 2. Only fix clearly escaped quotes that shouldn't be\n\t\t# But be very conservative - only if we're sure it's a Python string artifact\n\t\tif '\\\\\"' in js_code and js_code.count('\\\\\"') > js_code.count('\"'):\n\t\t\tjs_code = js_code.replace('\\\\\"', '\"')\n\t\tif \"\\\\'\" in js_code and js_code.count(\"\\\\'\") > js_code.count(\"'\"):\n\t\t\tjs_code = js_code.replace(\"\\\\'\", \"'\")\n\n\t\t# 3. Basic whitespace normalization only\n\t\tjs_code = js_code.strip()\n\n\t\t# Final validation - ensure it's not empty\n\t\tif not js_code:\n\t\t\traise ValueError('JavaScript code is empty after cleaning')\n\n\t\treturn js_code\n\n\tasync def screenshot(self, format: str = 'png', quality: int | None = None) -> str:\n\t\t\"\"\"Take a screenshot and return base64 encoded image.\n\n\t\tArgs:\n\t\t    format: Image format ('jpeg', 'png', 'webp')\n\t\t    quality: Quality 0-100 for JPEG format\n\n\t\tReturns:\n\t\t    Base64-encoded image data\n\t\t\"\"\"\n\t\tsession_id = await self._ensure_session()\n\n\t\tparams: 'CaptureScreenshotParameters' = {'format': format}\n\n\t\tif quality is not None and format.lower() == 'jpeg':","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/browser-use/browser-use/blob/6c73fced2f6d45a11d88622fe56365a5fe18f28b/browser_use/actor/page.py#L170-L206","documentation":"Raised by _fix_javascript_string when, after stripping whitespace, removing wrapper quotes, and un-escaping quotes, the resulting JavaScript string is empty. It is a guard so an empty expression is never sent to CDP.","triggerScenarios":"Passing an empty string or whitespace-only string to page.evaluate; passing a string that is only a pair of quotes ('\"\"' or \"''\"), which the quote-unwrapping step strips to nothing; passing a variable that is None-ish or was truncated to empty by upstream code.","commonSituations":"LLM tool call produces an evaluate action with empty code; f-string interpolation that evaluates to empty (f'({expr})' with expr=''); copying a placeholder from docs without filling in the body.","solutions":["Check the value before calling: if not js.strip(): raise/skip","Log the raw page_function at the call site to find where it became empty","If the code comes from an LLM action, add a non-empty constraint to the tool prompt"],"exampleFix":"# before\nawait page.evaluate(js_code)  # js_code may be ''\n\n# after\nif not js_code or not js_code.strip():\n    raise ValueError('evaluate called with empty script')\nawait page.evaluate(js_code)","handlingStrategy":"validation","validationCode":"if not isinstance(js, str) or not js.strip():\n    raise ValueError('refusing to evaluate empty script')","typeGuard":"def is_nonempty_script(js) -> bool:\n    return isinstance(js, str) and len(js.strip()) > 0","tryCatchPattern":"null","preventionTips":["Never pass unvalidated LLM-generated code straight to evaluate","Treat empty evaluate payloads as a caller bug, not a runtime condition"],"tags":["javascript","validation","empty-input"],"backgroundTag":null,"analyzedSha":"6c73fced2f6d45a11d88622fe56365a5fe18f28b","analyzedAt":"2026-08-14T19:42:40.557Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}