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
Fix proxy auth attach handler overwriting tab tracking
Laith Weinberger89817de3346a
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex b297de2df..938bcbf00 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1966,33 +1966,6 @@ class BrowserSession(BaseModel):except Exception as e:self.logger.debug(f'Failed to register authRequired handlers: {type(e).__name__}: {e}')- # Auto-enable Fetch on every newly attached target to ensure auth callbacks fire- def _on_attached(event: AttachedToTargetEvent, session_id: SessionID | None = None):- sid = event.get('sessionId') or event.get('session_id') or session_id- if not sid:- return-- async def _enable():- assert self._cdp_client_root- try:- await self._cdp_client_root.send.Fetch.enable(- params={'handleAuthRequests': True},- session_id=sid,- )- self.logger.debug(f'Fetch.enable(handleAuthRequests=True) enabled on attached session {sid}')- except Exception as e:- self.logger.debug(f'Fetch.enable on attached session failed: {type(e).__name__}: {e}')-- create_task_with_error_handling(- _enable(), name='fetch_enable_attached', logger_instance=self.logger, suppress_exceptions=True- )-- try:- self._cdp_client_root.register.Target.attachedToTarget(_on_attached)- self.logger.debug('Registered Target.attachedToTarget handler for Fetch.enable')- except Exception as e:- self.logger.debug(f'Failed to register attachedToTarget handler: {type(e).__name__}: {e}')-# Ensure Fetch is enabled for the current focused target's session, tootry:if self.agent_focus_target_id:diff --git a/browser_use/browser/session_manager.py b/browser_use/browser/session_manager.pyindex 91ccdadab..57ef256c0 100644--- a/browser_use/browser/session_manager.py+++ b/browser_use/browser/session_manager.py@@ -441,6 +441,21 @@ class SessionManager:# Add to sessions dictself._sessions[session_id] = cdp_session+ # If proxy auth is configured, enable Fetch auth handling on this session+ # Avoids overwriting Target.attachedToTarget handlers elsewhere+ try:+ proxy_cfg = self.browser_session.browser_profile.proxy+ username = proxy_cfg.username if proxy_cfg else None+ password = proxy_cfg.password if proxy_cfg else None+ if username and password:+ await cdp_session.cdp_client.send.Fetch.enable(+ params={'handleAuthRequests': True},+ session_id=cdp_session.session_id,+ )+ self.logger.debug(f'[SessionManager] Fetch.enable(handleAuthRequests=True) on session {session_id[:8]}...')+ except Exception as e:+ self.logger.debug(f'[SessionManager] Fetch.enable on attached session failed: {type(e).__name__}: {e}')+self.logger.debug(f'[SessionManager] Created session {session_id[:8]}... for target {target_id[:8]}... 'f'(total sessions: {len(self._sessions)})'