Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

added cookie overlay fix

Saurav Panda7833fcd137a6
diff --git a/browser_use/agent/system_prompts/system_prompt.md b/browser_use/agent/system_prompts/system_prompt.md
index 9af905048..f063d6621 100644
--- a/browser_use/agent/system_prompts/system_prompt.md
+++ b/browser_use/agent/system_prompts/system_prompt.md
@@ -154,10 +154,12 @@ Check the browser state each step to verify your previous action achieved its go
You can output multiple actions in one step. Try to be efficient where it makes sense. Do not predict actions which do not make sense for the current page.
**Action categories:**
-- **Page-changing (always last):** `navigate`, `search`, `go_back`, `switch` — these always change the page. Remaining actions after them are skipped automatically.
-- **Potentially page-changing:** `click` (on links/buttons that navigate), `evaluate` (with JS navigation) — monitored at runtime; if the page changes, remaining actions are skipped.
+- **Page-changing (always last):** `navigate`, `search`, `go_back`, `switch`, `evaluate` — these always change the page. Remaining actions after them are skipped automatically. Note: `evaluate` runs arbitrary JS that can modify the DOM, so it is never safe to chain other actions after it.
+- **Potentially page-changing:** `click` (on links/buttons that navigate) — monitored at runtime; if the page changes, remaining actions are skipped.
- **Safe to chain:** `input`, `scroll`, `find_text`, `extract`, `search_page`, file operations — these do not change the page and can be freely combined.
+**Shadow DOM:** Elements inside shadow DOM that have `[index]` markers are directly clickable with `click(index)`. Do NOT use `evaluate` to click them.
+
**Recommended combinations:**
- `input` + `input` + `input` + `click` → Fill multiple form fields then submit
- `input` + `input` → Fill multiple form fields
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 3e9a48b05..5381b023f 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -1841,7 +1841,8 @@ Context: {context}"""
return ActionResult(extracted_content=error_msg, long_term_memory=error_msg)
@self.registry.action(
- """Execute browser JavaScript. Best practice: wrap in IIFE (function(){...})() with try-catch for safety. Use ONLY browser APIs (document, window, DOM). NO Node.js APIs (fs, require, process). Example: (function(){try{const el=document.querySelector('#id');return el?el.value:'not found'}catch(e){return 'Error: '+e.message}})() Avoid comments. Use for hover, drag, zoom, custom selectors, extract/filter links, shadow DOM, or analysing page structure. Limit output size.""",
+ """Execute browser JavaScript. Best practice: wrap in IIFE (function(){...})() with try-catch for safety. Use ONLY browser APIs (document, window, DOM). NO Node.js APIs (fs, require, process). Example: (function(){try{const el=document.querySelector('#id');return el?el.value:'not found'}catch(e){return 'Error: '+e.message}})() Avoid comments. Use for hover, drag, zoom, custom selectors, extract/filter links, or analysing page structure. IMPORTANT: Shadow DOM elements with [index] markers can be clicked directly with click(index) — do NOT use evaluate() to click them. Only use evaluate for shadow DOM elements that are NOT indexed. Limit output size.""",
+ terminates_sequence=True,
)
async def evaluate(code: str, browser_session: BrowserSession):
# Execute JavaScript with proper error handling and promise support
diff --git a/tests/ci/test_multi_act_guards.py b/tests/ci/test_multi_act_guards.py
index 17b5e8ef1..1af94263f 100644
--- a/tests/ci/test_multi_act_guards.py
+++ b/tests/ci/test_multi_act_guards.py
@@ -146,6 +146,13 @@ class TestTerminatesSequenceMetadata:
assert action is not None
assert action.terminates_sequence is False
+ def test_evaluate_terminates(self, tools):
+ """evaluate() can mutate the DOM in unpredictable ways (e.g. dismiss cookie overlays),
+ so any actions queued after it should be skipped to avoid stale element references."""
+ action = tools.registry.registry.actions.get('evaluate')
+ assert action is not None
+ assert action.terminates_sequence is True
+
# ---------------------------------------------------------------------------
# 2. Static guard — navigate as non-last action skips remaining