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 tests and simplify pid detection
Nick Sweeting1b9f7253f0d7
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 833946df5..31565cd61 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -881,7 +881,7 @@ class BrowserSession(BaseModel):self.browser_pid = Nonereturn- self.cdp_url = self.cdp_url or f'http://localhost:{debug_port}/'+ self.cdp_url = self.cdp_url or f'http://127.0.0.1:{debug_port}/'# Wait for CDP port to become available (Chrome might still be starting)import httpx@@ -959,8 +959,6 @@ class BrowserSession(BaseModel):async def _unsafe_setup_new_browser_context(self) -> None:"""Unsafe browser context setup without retry protection."""- current_process = psutil.Process(os.getpid())- child_pids_before_launch = {child.pid for child in current_process.children(recursive=True)}# if we have a browser object but no browser_context, use the first context discovered or make a new oneif self.browser and not self.browser_context:@@ -1058,7 +1056,7 @@ class BrowserSession(BaseModel):# Launch chrome as subprocessself.logger.info(- f' ↳ Spawning Chrome subprocess listening on CDP port :{debug_port} with user_data_dir= {_log_pretty_path(self.browser_profile.user_data_dir)}'+ f' ↳ Spawning Chrome subprocess listening on CDP port 127.0.0.1:{debug_port} with user_data_dir= {_log_pretty_path(self.browser_profile.user_data_dir)}')process = await asyncio.create_subprocess_exec(*chrome_launch_cmd,@@ -1068,6 +1066,8 @@ class BrowserSession(BaseModel):# Store the browser PIDself.browser_pid = process.pid+ self._set_browser_keep_alive(False) # We launched it, so we should close it+ self.logger.debug(f'Chrome subprocess launched with PID {process.pid}')# Use the existing setup_browser_via_browser_pid method to connect# It will wait for the CDP port to become available@@ -1079,6 +1079,12 @@ class BrowserSession(BaseModel):if process.returncode is not None:raise RuntimeError(f'Chrome subprocess exited with code {process.returncode}')else:+ # Kill the subprocess if it's still running but we couldn't connect+ try:+ process.terminate()+ await process.wait()+ except Exception:+ passraise RuntimeError(f'Failed to connect to Chrome subprocess on port {debug_port}')except Exception as e:@@ -1148,74 +1154,7 @@ class BrowserSession(BaseModel):# ^ self.browser can unfortunately still be None at the end ^# playwright does not give us a browser object at all when we use launch_persistent_context()!- # Detect any new child chrome processes that we might have launched above- # Skip this if we already have a browser_pid from subprocess launching- if self.browser_pid:- self.logger.debug(- f'Skipping browser PID detection, already have browser_pid={self.browser_pid} from subprocess launch'- )- self._set_browser_keep_alive(False) # close the browser at the end because we launched it- else:-- def is_our_chrome_proc(pid: int) -> psutil.Process | None:- try:- proc = psutil.Process(pid)- cmdline = proc.cmdline()- if 'Helper' in proc.name():- return None- if proc.status() != 'running':- return None- if (- self.browser_profile.executable_path- and Path(cmdline[0]).expanduser().resolve()- != Path(self.browser_profile.executable_path).expanduser().resolve()- ):- # self.logger.debug(f'❌ Found new child chrome process that does not match our executable: {str(cmdline)[:50]}')- return None- if (- self.browser_profile.user_data_dir- and f'--user-data-dir={Path(self.browser_profile.user_data_dir).expanduser().resolve()}' in cmdline- ):- # self.logger.debug(f'✅ Found new child chrome process that matches our user_data_dir: {str(cmdline)[:50]}')- return proc- else:- # self.logger.debug(f'❌ Found new child chrome process that does not match our user_data_dir: {[arg for arg in cmdline if "--user-data-dir=" in arg]}')- return None- except Exception:- pass- return None-- child_pids_after_launch = {child.pid for child in current_process.children(recursive=True)}- new_child_pids = child_pids_after_launch - child_pids_before_launch- new_child_procs = list(filter(bool, (is_our_chrome_proc(pid) for pid in new_child_pids)))- if not new_child_procs:- self.logger.debug(- f'❌ Failed to find any new child chrome processes after launching new browser: {new_child_pids}'- )- new_chrome_proc = None- # Browser PID detection can fail in some environments (e.g. CI, containers)- # This is not critical - the browser is still running and usable- self.browser_pid = None- elif len(new_child_procs) > 1:- self.logger.debug(f'❌ Found multiple new child chrome processes after launching new browser: {new_child_procs}')- new_chrome_proc = None- self.browser_pid = None- else:- new_chrome_proc = new_child_procs[0]-- if new_chrome_proc and not self.browser_pid:- # look through the discovered new chrome processes to uniquely identify the one that *we* launched,- # match using unique user_data_dir- try:- self.browser_pid = new_chrome_proc.pid- cmdline = new_chrome_proc.cmdline()- executable_path = cmdline[0] if cmdline else 'unknown'- self.logger.info(f' ↳ Spawned browser_pid={self.browser_pid} {_log_pretty_path(executable_path)}')- if cmdline:- self.logger.debug(' '.join(cmdline)) # print the entire launch command for debugging- self._set_browser_keep_alive(False) # close the browser at the end because we launched it- except (psutil.NoSuchProcess, psutil.AccessDenied) as e:- self.logger.warning(f'Browser process {self.browser_pid} died immediately after launch: {type(e).__name__}')+ # PID detection is no longer needed since we get PIDs directly from subprocesses or passed objectsif self.browser:assert self.browser.is_connected(), (@@ -1629,6 +1568,9 @@ class BrowserSession(BaseModel):self.agent_current_page = Noneself.human_current_page = Noneself._cached_clickable_element_hashes = None+ # Reset CDP connection info when browser is stopped+ self.cdp_url = None+ self.browser_pid = Noneself._cached_browser_state_summary = None# Don't clear self.playwright here - it should be cleared explicitly in kill()diff --git a/browser_use/mcp/client.py b/browser_use/mcp/client.pyindex c7ca3506a..73876c5b0 100644--- a/browser_use/mcp/client.py+++ b/browser_use/mcp/client.py@@ -34,15 +34,11 @@ from browser_use.controller.service import Controllerlogger = logging.getLogger(__name__)-# Try to import MCP SDK-try:- from mcp import ClientSession, StdioServerParameters, types- from mcp.client.stdio import stdio_client+# Import MCP SDK+from mcp import ClientSession, StdioServerParameters, types+from mcp.client.stdio import stdio_client- MCP_AVAILABLE = True-except ImportError:- MCP_AVAILABLE = False- logger.warning('MCP SDK not installed. Install with: pip install mcp')+MCP_AVAILABLE = Trueclass MCPClient:@@ -63,9 +59,6 @@ class MCPClient:args: Arguments for the command (e.g., ["@playwright/mcp@latest"])env: Environment variables for the server process"""- if not MCP_AVAILABLE:- raise ImportError('MCP SDK not installed. Install with: pip install mcp')-self.server_name = server_nameself.command = commandself.args = args or []diff --git a/tests/ci/test_agent_sensitive_data.py b/tests/ci/test_agent_sensitive_data.pyindex 09d5454b4..26792671b 100644--- a/tests/ci/test_agent_sensitive_data.py+++ b/tests/ci/test_agent_sensitive_data.py@@ -67,7 +67,7 @@ def test_replace_sensitive_data_with_missing_keys(registry, caplog):assert 'user123' in result.textassert '<secret>password</secret>' in result.text# Verify the behavior - username replaced, password kept as tag- assert 'password' in caplog.text+ assert 'Missing' in caplog.text and 'password' in caplog.textcaplog.clear()# Case 3: Multiple keys missing@@ -85,7 +85,7 @@ def test_replace_sensitive_data_with_missing_keys(registry, caplog):assert 'user123' in result.textassert '<secret>password</secret>' in result.text# Empty value should be treated the same as missing key- assert 'password' in caplog.text+ assert 'Missing' in caplog.text and 'password' in caplog.textcaplog.clear()# Restore original propagate settingdiff --git a/tests/ci/test_mcp_client.py b/tests/ci/test_mcp_client.pyindex 435573e0a..ec484acf0 100644--- a/tests/ci/test_mcp_client.py+++ b/tests/ci/test_mcp_client.py@@ -17,7 +17,7 @@ from browser_use import Agent, BrowserProfile, BrowserSession, Controllerfrom browser_use.mcp.client import MCPClient-class TestMCPServer:+class MockMCPServer:"""A minimal MCP server for testing."""def __init__(self):@@ -110,10 +110,10 @@ import asyncioimport syssys.path.insert(0, "{project_root}")-from tests.ci.test_mcp_client import TestMCPServer+from tests.ci.test_mcp_client import MockMCPServerasync def main():- server = TestMCPServer()+ server = MockMCPServer()await server.run()if __name__ == "__main__":