Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Enhance JavaScript execution guidelines in service.py with critical syntax rules and proper examples for IIFE wrapping, error handling, and shadow DOM access.
Magnus Müller8692dc8bd173
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.pyindex 341789820..4fc0a2f0d 100644--- a/browser_use/tools/service.py+++ b/browser_use/tools/service.py@@ -942,20 +942,52 @@ You will be given a query and the markdown of a webpage that has been filtered t@self.registry.action("""This JavaScript code gets executed with Runtime.evaluate and 'returnByValue': True, 'awaitPromise': True++CRITICAL SYNTAX RULES - FAILURE TO FOLLOW CAUSES "Uncaught at line 0" ERRORS:+- ALWAYS wrap your code in IIFE: (function(){ ... })() or (async function(){ ... })() for async code+- ALWAYS add try-catch blocks to prevent execution errors+- ALWAYS use proper semicolons and valid JavaScript syntax+- NEVER write multiline code without proper IIFE wrapping+- ALWAYS validate elements exist before accessing them+EXAMPLES:Using when other tools do not achieve the expected change or when a more general tool is needed, e.g. for filling a form all at once, hovering, dragging, extracting only links, extracting content from the page, press and hold, hovoring, clicking on coordinates, zooming, use this if the user provides custom selecotrs which you can otherwise not interact with ....You can also use it to explore the website.- Write code to solve problems you could not solve with other tools.- Don't write comments in here, no human reads that.-- Write only valid js code.+- Write only valid js code.- use this to e.g. extract + filter links, convert the page to json into the format you need etc...-- wrap your code in (function(){ ... })() or (async function(){ ... })() for async code-- wrap your code in a try catch block++- limit the output otherwise your context will explode- think if you deal with special elements like iframes / shadow roots etc- Adopt your strategy for React Native Web, React, Angular, Vue, MUI pages etc.- e.g. with synthetic events, keyboard simulation, shadow DOM, etc.+PROPER SYNTAX EXAMPLES:+CORRECT: (function(){ try { const el = document.querySelector('#id'); return el ? el.value : 'not found'; } catch(e) { return 'Error: ' + e.message; } })()+CORRECT: (async function(){ try { await new Promise(r => setTimeout(r, 100)); return 'done'; } catch(e) { return 'Error: ' + e.message; } })()++WRONG: const el = document.querySelector('#id'); el ? el.value : '';+WRONG: document.querySelector('#id').value+WRONG: Multiline code without IIFE wrapping++SHADOW DOM ACCESS EXAMPLE:+(function(){+ try {+ const hosts = document.querySelectorAll('*');+ for (let host of hosts) {+ if (host.shadowRoot) {+ const el = host.shadowRoot.querySelector('#target');+ if (el) return el.textContent;+ }+ }+ return 'Not found';+ } catch(e) {+ return 'Error: ' + e.message;+ }+})()+## Return values:- Async functions (with await, promises, timeouts) are automatically handled- Returns strings, numbers, booleans, and serialized objects/arrays