Skip to content

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

Enhance tab identification logic in prompts.py for improved accuracy and clarity. Update pyproject.toml to include a missing comma in dev-dependencies.

Magnus Müller8a09999e3b89
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index cb5ef98fa..a7106f587 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -111,10 +111,23 @@ class AgentMessagePrompt:
elements_text = 'empty page'
tabs_text = ''
+ current_tab_candidates = []
+
+ # Find tabs that match both URL and title to identify current tab more reliably
for tab in self.browser_state.tabs:
- tabs_text += f'Tab {tab.page_id}: URL={tab.url}, Title={tab.title[:30]}\n'
+ if tab.url == self.browser_state.url and tab.title == self.browser_state.title:
+ current_tab_candidates.append(tab.page_id)
+
+ # If we have exactly one match, mark it as current
+ # Otherwise, don't mark any tab as current to avoid confusion
+ current_tab_id = current_tab_candidates[0] if len(current_tab_candidates) == 1 else None
+
+ for tab in self.browser_state.tabs:
+ tabs_text += f'Tab {tab.page_id}: {tab.url} - {tab.title[:30]}\n'
+
+ current_tab_text = f'Current tab: {current_tab_id}' if current_tab_id is not None else ''
- browser_state = f"""Current URL: {self.browser_state.url}
+ browser_state = f"""{current_tab_text}
Available tabs:
{tabs_text}
Interactive elements from top layer of the current page inside the viewport{truncated_text}:
diff --git a/pyproject.toml b/pyproject.toml
index 269c9e028..2748d863d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -164,5 +164,5 @@ dev-dependencies = [
"pyright>=1.1.399",
"ty>=0.0.1a1",
# "pytest-playwright-asyncio>=0.7.0", # not actually needed I think
- "pytest-xdist>=3.7.0"
+ "pytest-xdist>=3.7.0",
]