Skip to content

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

Only switch tab based on target it

Magnus Müller35fe73cba1b3
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index bd9e73b09..492c8c2c5 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -497,12 +497,7 @@ class Tools(Generic[Context]):
async def switch_tab(params: SwitchTabAction, browser_session: BrowserSession):
# Dispatch switch tab event
try:
- if params.tab_id:
- target_id = await browser_session.get_target_id_from_tab_id(params.tab_id)
- elif params.url:
- target_id = await browser_session.get_target_id_from_url(params.url)
- else:
- target_id = await browser_session.get_most_recently_opened_target_id()
+ target_id = await browser_session.get_target_id_from_tab_id(params.tab_id)
event = browser_session.event_bus.dispatch(SwitchTabEvent(target_id=target_id))
await event
@@ -514,7 +509,7 @@ class Tools(Generic[Context]):
except Exception as e:
logger.error(f'Failed to switch tab: {type(e).__name__}: {e}')
clean_msg = extract_llm_error_message(e)
- return ActionResult(error=f'Failed to switch to tab {params.tab_id or params.url}: {clean_msg}')
+ return ActionResult(error=f'Failed to switch to tab {params.tab_id}: {clean_msg}')
@self.registry.action('Close an existing tab', param_model=CloseTabAction)
async def close_tab(params: CloseTabAction, browser_session: BrowserSession):
diff --git a/browser_use/tools/views.py b/browser_use/tools/views.py
index a4d7a678b..c25c47c2a 100644
--- a/browser_use/tools/views.py
+++ b/browser_use/tools/views.py
@@ -43,15 +43,10 @@ class StructuredOutputAction(BaseModel, Generic[T]):
class SwitchTabAction(BaseModel):
- url: str | None = Field(
- default=None,
- description='URL or URL substring of the tab to switch to, if not provided, the tab_id or most recently opened tab will be used',
- )
- tab_id: str | None = Field(
- default=None,
+ tab_id: str = Field(
min_length=4,
max_length=4,
- description='exact 4 character Tab ID to match instead of URL, prefer using this if known',
+ description='Last 4 chars of TargetID',
) # last 4 chars of TargetID