Skip to content

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

add new test for highly parallel browser launching

Nick Sweeting46f6a2646f51
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 4b6746732..94845f35d 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -878,41 +878,59 @@ class BrowserSession(BaseModel):
# 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
- try:
- 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 = [psutil.Process(pid) for pid in new_child_pids]
- new_chrome_procs = [
- proc
- for proc in new_child_procs
- if 'Helper' not in proc.name()
- and proc.status() == 'running'
- and proc.cmdline()[0] == self.browser_profile.executable_path # require executable to match
- and f'--user-data-dir={self.browser_profile.user_data_dir}'
- in ' '.join(proc.cmdline()) # require --user-data-dir to match
- ]
- except Exception as e:
- self.logger.debug(
- f'❌ Error trying to find child chrome processes after launching new browser: {type(e).__name__}: {e}'
- )
- new_chrome_procs = []
+ 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
- if new_chrome_procs and not self.browser_pid:
+ 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
+ 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
+ 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_procs[0].pid
- cmdline = new_chrome_procs[0].cmdline()
+ 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} disappeared immediately after launch: {type(e).__name__}'
- )
+ self.logger.warning(f'Browser process {self.browser_pid} died immediately after launch: {type(e).__name__}')
if self.browser:
assert self.browser.is_connected(), (
@@ -1451,22 +1469,22 @@ class BrowserSession(BaseModel):
try:
await page.evaluate(
"""
- try {
- // Remove the highlight container and all its contents
- const container = document.getElementById('playwright-highlight-container');
- if (container) {
- container.remove();
- }
-
- // Remove highlight attributes from elements
- const highlightedElements = document.querySelectorAll('[browser-user-highlight-id^="playwright-highlight-"]');
- highlightedElements.forEach(el => {
- el.removeAttribute('browser-user-highlight-id');
- });
- } catch (e) {
- console.error('Failed to remove highlights:', e);
- }
- """
+ try {
+ // Remove the highlight container and all its contents
+ const container = document.getElementById('playwright-highlight-container');
+ if (container) {
+ container.remove();
+ }
+
+ // Remove highlight attributes from elements
+ const highlightedElements = document.querySelectorAll('[browser-user-highlight-id^="playwright-highlight-"]');
+ highlightedElements.forEach(el => {
+ el.removeAttribute('browser-user-highlight-id');
+ });
+ } catch (e) {
+ console.error('Failed to remove highlights:', e);
+ }
+ """
)
except Exception as e:
self.logger.debug(f'⚠️ Failed to remove highlights (this is usually ok): {type(e).__name__}: {e}')
@@ -2302,9 +2320,9 @@ class BrowserSession(BaseModel):
Parameters:
-----------
cache_clickable_elements_hashes: bool
- If True, cache the clickable elements hashes for the current state.
- This is used to calculate which elements are new to the LLM since the last message,
- which helps reduce token usage.
+ If True, cache the clickable elements hashes for the current state.
+ This is used to calculate which elements are new to the LLM since the last message,
+ which helps reduce token usage.
"""
await self._wait_for_page_and_frames_load()
updated_state = await self._get_updated_state()
@@ -2587,10 +2605,10 @@ class BrowserSession(BaseModel):
Creates a CSS selector for a DOM element, handling various edge cases and special characters.
Args:
- element: The DOM element to create a selector for
+ element: The DOM element to create a selector for
Returns:
- A valid CSS selector string
+ A valid CSS selector string
"""
try:
# Get base selector from XPath
@@ -3032,7 +3050,7 @@ class BrowserSession(BaseModel):
await new_page.goto(url, wait_until='domcontentloaded')
await self._wait_for_page_and_frames_load(timeout_overwrite=1)
except Exception as e:
- self.logger.error(f'❌ Error navigating to {url}: {type(e).__name__}: {e}')
+ self.logger.error(f'❌ Error navigating to {url}: {type(e).__name__}: {e} (proceeding anyway...)')
assert self.human_current_page is not None
assert self.agent_current_page is not None
diff --git a/tests/ci/test_browser_session_start.py b/tests/ci/test_browser_session_start.py
index 7c682af2a..169fc3276 100644
--- a/tests/ci/test_browser_session_start.py
+++ b/tests/ci/test_browser_session_start.py
@@ -12,8 +12,8 @@ Tests cover:
import asyncio
import json
import logging
-from pathlib import Path
import tempfile
+from pathlib import Path
import pytest
@@ -665,7 +665,6 @@ class TestBrowserSessionReusePatterns:
async def test_parallel_agents_same_browser_multiple_tabs(self, httpserver):
"""Test Parallel Agents, Same Browser, Multiple Tabs pattern"""
- import tempfile
from browser_use import Agent, BrowserSession
@@ -843,7 +842,6 @@ class TestBrowserSessionReusePatterns:
async def test_parallel_agents_same_profile_different_browsers(self, mock_llm):
"""Test Parallel Agents, Same Profile, Different Browsers pattern (recommended)"""
- import tempfile
from browser_use import Agent
from browser_use.browser import BrowserProfile, BrowserSession
@@ -945,3 +943,83 @@ class TestBrowserSessionReusePatterns:
await browser_session1.browser_context.pages[0].evaluate('alert(1)')
await browser_session1.kill()
+
+ async def test_many_parallel_browser_sessions(self):
+ """Test spawning 20 parallel browser_sessions with different settings and ensure they all work"""
+ from browser_use import BrowserSession
+
+ browser_sessions = []
+
+ for i in range(5):
+ browser_sessions.append(
+ BrowserSession(
+ browser_profile=BrowserProfile(
+ user_data_dir=None,
+ headless=True,
+ keep_alive=True,
+ ),
+ )
+ )
+ for i in range(5):
+ browser_sessions.append(
+ BrowserSession(
+ browser_profile=BrowserProfile(
+ user_data_dir=Path(tempfile.mkdtemp(prefix=f'browseruse-tmp-{i}')),
+ headless=True,
+ keep_alive=True,
+ ),
+ )
+ )
+ for i in range(5):
+ browser_sessions.append(
+ BrowserSession(
+ browser_profile=BrowserProfile(
+ user_data_dir=None,
+ headless=True,
+ keep_alive=False,
+ ),
+ )
+ )
+ for i in range(5):
+ browser_sessions.append(
+ BrowserSession(
+ browser_profile=BrowserProfile(
+ user_data_dir=Path(tempfile.mkdtemp(prefix=f'browseruse-tmp-{i}')),
+ headless=True,
+ keep_alive=False,
+ ),
+ )
+ )
+
+ await asyncio.gather(*[browser_session.start() for browser_session in browser_sessions])
+
+ # ensure all are connected and usable
+ new_tab_tasks = []
+ for browser_session in browser_sessions:
+ assert browser_session.is_connected()
+ assert browser_session.browser_context is not None
+ new_tab_tasks.append(browser_session.create_new_tab('chrome://version'))
+ await asyncio.gather(*new_tab_tasks)
+
+ # kill every 3rd browser_session
+ kill_tasks = []
+ for i in range(0, len(browser_sessions), 3):
+ kill_tasks.append(browser_sessions[i].kill())
+ browser_sessions[i] = None
+ await asyncio.gather(*kill_tasks)
+
+ # ensure the remaining browser_sessions are still connected and usable
+ new_tab_tasks = []
+ screenshot_tasks = []
+ for browser_session in filter(bool, browser_sessions):
+ assert browser_session.is_connected()
+ assert browser_session.browser_context is not None
+ new_tab_tasks.append(browser_session.create_new_tab('chrome://version'))
+ screenshot_tasks.append(browser_session.take_screenshot())
+ await asyncio.gather(*new_tab_tasks)
+ await asyncio.gather(*screenshot_tasks)
+
+ kill_tasks = []
+ for browser_session in filter(bool, browser_sessions):
+ kill_tasks.append(browser_session.kill())
+ await asyncio.gather(*kill_tasks)