Skip to content

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

refactor: streamline tab switching and navigation handling in BrowserSession

reformedot2de09c1e5328
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 8a69949bc..9666db0de 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -685,49 +685,49 @@ class BrowserSession(BaseModel):
# Use current tab
target_id = target_id or current_target_id
- # Only switch tab if we're not already on the target tab
- if self.agent_focus_target_id is None or self.agent_focus_target_id != target_id:
- self.logger.debug(
- f'[on_NavigateToUrlEvent] Switching to target tab {target_id[-4:]} (current: {self.agent_focus_target_id[-4:] if self.agent_focus_target_id else "none"})'
- )
- # Activate target (bring to foreground)
- await self.event_bus.dispatch(SwitchTabEvent(target_id=target_id))
- # which updates agent_focus_target_id for us
- else:
- self.logger.debug(f'[on_NavigateToUrlEvent] Already on target tab {target_id[-4:]}, skipping SwitchTabEvent')
-
- assert self.agent_focus_target_id is not None and self.agent_focus_target_id == target_id, (
- 'Agent focus not updated to new target_id after SwitchTabEvent should have switched to it'
+ # Switch to target tab if needed (for both new_tab=True and new_tab=False)
+ if self.agent_focus_target_id is None or self.agent_focus_target_id != target_id:
+ self.logger.debug(
+ f'[on_NavigateToUrlEvent] Switching to target tab {target_id[-4:]} (current: {self.agent_focus_target_id[-4:] if self.agent_focus_target_id else "none"})'
)
+ # Activate target (bring to foreground)
+ await self.event_bus.dispatch(SwitchTabEvent(target_id=target_id))
+ # which updates agent_focus_target_id for us
+ else:
+ self.logger.debug(f'[on_NavigateToUrlEvent] Already on target tab {target_id[-4:]}, skipping SwitchTabEvent')
- # Dispatch navigation started
- await self.event_bus.dispatch(NavigationStartedEvent(target_id=target_id, url=event.url))
+ assert self.agent_focus_target_id is not None and self.agent_focus_target_id == target_id, (
+ 'Agent focus not updated to new target_id after SwitchTabEvent should have switched to it'
+ )
- # Navigate to URL with proper lifecycle waiting
- await self._navigate_and_wait(event.url, target_id)
+ # Dispatch navigation started
+ await self.event_bus.dispatch(NavigationStartedEvent(target_id=target_id, url=event.url))
- # Close any extension options pages that might have opened
- await self._close_extension_options_pages()
+ # Navigate to URL with proper lifecycle waiting
+ await self._navigate_and_wait(event.url, target_id)
- # Dispatch navigation complete
- self.logger.debug(f'Dispatching NavigationCompleteEvent for {event.url} (tab #{target_id[-4:]})')
- await self.event_bus.dispatch(
- NavigationCompleteEvent(
- target_id=target_id,
- url=event.url,
- status=None, # CDP doesn't provide status directly
- )
+ # Close any extension options pages that might have opened
+ await self._close_extension_options_pages()
+
+ # Dispatch navigation complete
+ self.logger.debug(f'Dispatching NavigationCompleteEvent for {event.url} (tab #{target_id[-4:]})')
+ await self.event_bus.dispatch(
+ NavigationCompleteEvent(
+ target_id=target_id,
+ url=event.url,
+ status=None, # CDP doesn't provide status directly
)
- await self.event_bus.dispatch(
- AgentFocusChangedEvent(target_id=target_id, url=event.url)
- ) # do not await! AgentFocusChangedEvent calls SwitchTabEvent and it will deadlock, dispatch to enqueue and return
-
- # Note: These should be handled by dedicated watchdogs:
- # - Security checks (security_watchdog)
- # - Page health checks (crash_watchdog)
- # - Dialog handling (dialog_watchdog)
- # - Download handling (downloads_watchdog)
- # - DOM rebuilding (dom_watchdog)
+ )
+ await self.event_bus.dispatch(
+ AgentFocusChangedEvent(target_id=target_id, url=event.url)
+ ) # do not await! AgentFocusChangedEvent calls SwitchTabEvent and it will deadlock, dispatch to enqueue and return
+
+ # Note: These should be handled by dedicated watchdogs:
+ # - Security checks (security_watchdog)
+ # - Page health checks (crash_watchdog)
+ # - Dialog handling (dialog_watchdog)
+ # - Download handling (downloads_watchdog)
+ # - DOM rebuilding (dom_watchdog)
except Exception as e:
self.logger.error(f'Navigation failed: {type(e).__name__}: {e}')