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
lint
Nick Sweeting03ea20826d1e
diff --git a/tests/ci/test_action_ClickElementEvent.py b/tests/ci/test_action_ClickElementEvent.pyindex ec160df61..d3e151f29 100644--- a/tests/ci/test_action_ClickElementEvent.py+++ b/tests/ci/test_action_ClickElementEvent.py@@ -199,7 +199,6 @@ class TestClickElementEvent:result_text = result_js.get('result', {}).get('value', '')assert result_text == expected_result_text, f"Expected result text '{expected_result_text}', got '{result_text}'"-async def test_click_element_new_tab(self, controller, browser_session, base_url, http_server):"""Test that click_element_by_index with new_tab=True opens links in new tabs."""# Add route for new tab test page@@ -373,44 +372,43 @@ class TestClickElementEvent:# Navigate to the pagegoto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/inline_offscreen', new_tab=False)}-+from browser_use.agent.views import ActionModel-+class GoToUrlActionModel(ActionModel):go_to_url: GoToUrlAction | None = None-+await controller.act(GoToUrlActionModel(**goto_action), browser_session)await asyncio.sleep(0.5)-+# Get the clickable elementsawait browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)selector_map = await browser_session.get_selector_map()-+# Find the inline elementinline_index = Nonefor idx, element in selector_map.items():if 'inline-link' in str(element.attributes.get('class', '')):inline_index = idxbreak-+assert inline_index is not None, 'Could not find inline element'-+# Click the element - should click the visible portionclass ClickActionModel(ActionModel):click_element_by_index: ClickElementAction | None = None-+result = await controller.act(- ClickActionModel(click_element_by_index=ClickElementAction(index=inline_index)),- browser_session+ ClickActionModel(click_element_by_index=ClickElementAction(index=inline_index)), browser_session)-+assert result.error is None, f'Click failed: {result.error}'-+# Verify click worked using CDPcdp_session = await browser_session.get_or_create_cdp_session()result_js = await browser_session.cdp_client.send.Runtime.evaluate(params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},- session_id=cdp_session.session_id+ session_id=cdp_session.session_id,)assert result_js.get('result', {}).get('value') == 'Inline clicked'@@ -458,44 +456,43 @@ class TestClickElementEvent:# Navigate to the pagegoto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/block_in_inline', new_tab=False)}-+from browser_use.agent.views import ActionModel-+class GoToUrlActionModel(ActionModel):go_to_url: GoToUrlAction | None = None-+await controller.act(GoToUrlActionModel(**goto_action), browser_session)await asyncio.sleep(0.5)-+# Get the clickable elementsawait browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)selector_map = await browser_session.get_selector_map()-+# Find the block element inside inlineblock_index = Nonefor idx, element in selector_map.items():if 'block-inside' in str(element.attributes.get('class', '')):block_index = idxbreak-+assert block_index is not None, 'Could not find block element'-+# Click the block elementclass ClickActionModel(ActionModel):click_element_by_index: ClickElementAction | None = None-+result = await controller.act(- ClickActionModel(click_element_by_index=ClickElementAction(index=block_index)),- browser_session+ ClickActionModel(click_element_by_index=ClickElementAction(index=block_index)), browser_session)-+assert result.error is None, f'Click failed: {result.error}'-+# Verify click workedcdp_session = await browser_session.get_or_create_cdp_session()result_js = await browser_session.cdp_client.send.Runtime.evaluate(params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},- session_id=cdp_session.session_id+ session_id=cdp_session.session_id,)assert result_js.get('result', {}).get('value') == 'Block clicked'@@ -549,44 +546,43 @@ class TestClickElementEvent:# Navigate to the pagegoto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/covered_element', new_tab=False)}-+from browser_use.agent.views import ActionModel-+class GoToUrlActionModel(ActionModel):go_to_url: GoToUrlAction | None = None-+await controller.act(GoToUrlActionModel(**goto_action), browser_session)await asyncio.sleep(0.5)-+# Get the clickable elementsawait browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)selector_map = await browser_session.get_selector_map()-+# Find the target elementtarget_index = Nonefor idx, element in selector_map.items():if 'target' in str(element.attributes.get('class', '')):target_index = idxbreak-+assert target_index is not None, 'Could not find target element'-+# Click should still work on the visible portionclass ClickActionModel(ActionModel):click_element_by_index: ClickElementAction | None = None-+result = await controller.act(- ClickActionModel(click_element_by_index=ClickElementAction(index=target_index)),- browser_session+ ClickActionModel(click_element_by_index=ClickElementAction(index=target_index)), browser_session)-+assert result.error is None, f'Click failed: {result.error}'-+# Verify click workedcdp_session = await browser_session.get_or_create_cdp_session()result_js = await browser_session.cdp_client.send.Runtime.evaluate(params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},- session_id=cdp_session.session_id+ session_id=cdp_session.session_id,)assert result_js.get('result', {}).get('value') == 'Target clicked'@@ -612,19 +608,19 @@ class TestClickElementEvent:# Navigate to the pagegoto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/file_input', new_tab=False)}-+from browser_use.agent.views import ActionModel-+class GoToUrlActionModel(ActionModel):go_to_url: GoToUrlAction | None = None-+await controller.act(GoToUrlActionModel(**goto_action), browser_session)await asyncio.sleep(0.5)-+# Get the clickable elementsawait browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)selector_map = await browser_session.get_selector_map()-+# Find the file inputfile_input_index = Nonefor idx, element in selector_map.items():@@ -632,22 +628,22 @@ class TestClickElementEvent:if element.attributes and element.attributes.get('type') == 'file':file_input_index = idxbreak-+assert file_input_index is not None, 'Could not find file input element'-+# Attempt to click should raise an exceptionclass ClickActionModel(ActionModel):click_element_by_index: ClickElementAction | None = None-+result = await controller.act(- ClickActionModel(click_element_by_index=ClickElementAction(index=file_input_index)),- browser_session+ ClickActionModel(click_element_by_index=ClickElementAction(index=file_input_index)), browser_session)-+# Should have an error about file inputsassert result.error is not None, 'Expected error for file input click'- assert 'file input' in result.error.lower() or 'file upload' in result.error.lower(), \+ assert 'file input' in result.error.lower() or 'file upload' in result.error.lower(), (f'Error message should mention file input, got: {result.error}'+ )async def test_select_dropdown_click_prevention(self, controller, browser_session, base_url, http_server):"""Test that clicking a select dropdown element raises an exception."""@@ -675,38 +671,38 @@ class TestClickElementEvent:# Navigate to the pagegoto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/select_dropdown', new_tab=False)}-+from browser_use.agent.views import ActionModel-+class GoToUrlActionModel(ActionModel):go_to_url: GoToUrlAction | None = None-+await controller.act(GoToUrlActionModel(**goto_action), browser_session)await asyncio.sleep(0.5)-+# Get the clickable elementsawait browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)selector_map = await browser_session.get_selector_map()-+# Find the select elementselect_index = Nonefor idx, element in selector_map.items():if element.tag_name and element.tag_name.lower() == 'select':select_index = idxbreak-+assert select_index is not None, 'Could not find select element'-+# Attempt to click should raise an exceptionclass ClickActionModel(ActionModel):click_element_by_index: ClickElementAction | None = None-+result = await controller.act(- ClickActionModel(click_element_by_index=ClickElementAction(index=select_index)),- browser_session+ ClickActionModel(click_element_by_index=ClickElementAction(index=select_index)), browser_session)-+# Should have an error about select elementsassert result.error is not None, 'Expected error for select element click'- assert 'select' in result.error.lower() and 'dropdown' in result.error.lower(), \+ assert 'select' in result.error.lower() and 'dropdown' in result.error.lower(), (f'Error message should mention select/dropdown, got: {result.error}'+ )diff --git a/tests/ci/test_action_NavigateToUrlEvent.py b/tests/ci/test_action_NavigateToUrlEvent.pyindex b610b156a..6ea9cbb22 100644--- a/tests/ci/test_action_NavigateToUrlEvent.py+++ b/tests/ci/test_action_NavigateToUrlEvent.py@@ -167,7 +167,7 @@ class TestNavigateToUrlEvent:# Verify navigation workedassert isinstance(result, ActionResult)assert result.extracted_content is not None-+# Check we're on the right pagecurrent_url = await browser_session.get_current_page_url()assert f'{base_url}/page1' in current_url@@ -193,7 +193,7 @@ class TestNavigateToUrlEvent:"""Test navigating to a data: URL."""# Create a simple data URLdata_url = 'data:text/html,<html><head><title>Data URL Test</title></head><body><h1>Data URL Content</h1></body></html>'-+action_data = {'go_to_url': GoToUrlAction(url=data_url, new_tab=False)}class GoToUrlActionModel(ActionModel):@@ -314,7 +314,7 @@ class TestNavigateToUrlEvent:# Try to navigate to a URL that will likely timeout# Using a private IP that's unlikely to respondtimeout_url = 'http://192.0.2.1:8080/timeout'-+action_data = {'go_to_url': GoToUrlAction(url=timeout_url, new_tab=False)}class GoToUrlActionModel(ActionModel):@@ -322,7 +322,7 @@ class TestNavigateToUrlEvent:# This should complete without hanging indefinitelyresult = await controller.act(GoToUrlActionModel(**action_data), browser_session)-+# Should get a result (possibly with error)assert isinstance(result, ActionResult)@@ -355,20 +355,20 @@ class TestNavigateToUrlEvent:async def test_navigate_to_url_event_with_new_tab_and_tab_created_event(self, browser_session, base_url):"""Test NavigateToUrlEvent with new_tab=True and verify TabCreatedEvent is emitted."""from browser_use.browser.events import NavigateToUrlEvent, TabCreatedEvent-+initial_tab_count = len(browser_session.tabs)-+# Navigate to URL in new tab via direct eventnav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/page2', new_tab=True))await nav_event-+# Verify new tab was createdassert len(browser_session.tabs) == initial_tab_count + 1-+# Check that current page is the new tabcurrent_url = await browser_session.get_current_page_url()assert f'{base_url}/page2' in current_url-+# Check event history for TabCreatedEventevent_history = list(browser_session.event_bus.event_history.values())created_events = [e for e in event_history if isinstance(e, TabCreatedEvent)]@@ -377,26 +377,24 @@ class TestNavigateToUrlEvent:async def test_navigate_with_new_tab_focuses_properly(self, browser_session):"""Test that NavigateToUrlEvent with new_tab=True properly switches focus."""from browser_use.browser.events import NavigateToUrlEvent-+# Get initial stateinitial_tabs_count = len(browser_session.tabs)initial_url = await browser_session.get_current_page_url()-+# Navigate to a URL in a new tab- nav_event = browser_session.event_bus.dispatch(- NavigateToUrlEvent(url='https://example.com', new_tab=True)- )+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url='https://example.com', new_tab=True))await nav_event-+# Small delay to ensure navigation completesawait asyncio.sleep(1)-+# Get browser state after navigationcurrent_url = await browser_session.get_current_page_url()-+# Verify a new tab was createdassert len(browser_session.tabs) == initial_tabs_count + 1-+# Verify focus switched to the new tabassert 'example.com' in current_urlassert current_url != initial_url@@ -404,23 +402,23 @@ class TestNavigateToUrlEvent:async def test_navigate_and_verify_page_properties(self, browser_session, base_url):"""Test that NavigateToUrlEvent changes the URL and page properties are accessible."""from browser_use.browser.events import NavigateToUrlEvent-+# Navigate to the test pageevent = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await event-+# Wait for navigation to completeawait asyncio.sleep(0.5)-+# Get the current page URLcurrent_url = await browser_session.get_current_page_url()-+# Verify the page URL matches what we navigated toassert f'{base_url}/' in current_url-+# Get the actual page objectpage = browser_session.page-+# Verify the page titletitle = await page.title()- assert title == 'Test Home Page'\ No newline at end of file+ assert title == 'Test Home Page'diff --git a/tests/ci/test_agent_shutdown.py b/tests/ci/test_agent_shutdown.pyindex 9fa70bc51..62a20d2ed 100644--- a/tests/ci/test_agent_shutdown.py+++ b/tests/ci/test_agent_shutdown.py@@ -4,7 +4,8 @@ import asyncioimport loggingimport threadingimport time-from browser_use import Agent, BrowserSession, BrowserProfile++from browser_use import Agent, BrowserProfile, BrowserSessionfrom tests.ci.conftest import create_mock_llmlogger = logging.getLogger(__name__)@@ -12,33 +13,28 @@ logger = logging.getLogger(__name__)async def test_agent_exits_within_10s():"""Test that agent with done() exits within 10 seconds."""- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False- )- )+ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))await browser_session.start()-+# Create mock LLM that returns done immediately (default behavior)mock_llm = create_mock_llm()-+agent = Agent(- task="Test task",+ task='Test task',llm=mock_llm,browser_session=browser_session,)-+start_time = time.time()result = await agent.run(max_steps=1)exit_time = time.time() - start_time-- assert result.is_done(), "Agent should have completed"- assert result.is_successful(), "Agent should have succeeded"- assert exit_time < 10, f"Agent took {exit_time:.2f}s to exit (should be < 10s)"-++ assert result.is_done(), 'Agent should have completed'+ assert result.is_successful(), 'Agent should have succeeded'+ assert exit_time < 10, f'Agent took {exit_time:.2f}s to exit (should be < 10s)'+# Verify browser session was cleaned up- assert browser_session._cdp_client_root is None, "Browser session should be cleaned up"+ assert browser_session._cdp_client_root is None, 'Browser session should be cleaned up'async def test_no_leaked_asyncio_tasks():@@ -46,116 +42,98 @@ async def test_no_leaked_asyncio_tasks():# Get initial tasksinitial_tasks = asyncio.all_tasks(asyncio.get_event_loop())initial_count = len(initial_tasks)-- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False- )- )++ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))await browser_session.start()-+mock_llm = create_mock_llm()-+agent = Agent(- task="Test task",+ task='Test task',llm=mock_llm,browser_session=browser_session,)-+await agent.run(max_steps=1)-+# Wait briefly for cleanupawait asyncio.sleep(1)-+# Get final tasksfinal_tasks = asyncio.all_tasks(asyncio.get_event_loop())final_count = len(final_tasks)-+# Allow some tolerance for framework tasks- assert final_count <= initial_count + 1, f"Too many tasks remain: {final_count} (initial: {initial_count})"+ assert final_count <= initial_count + 1, f'Too many tasks remain: {final_count} (initial: {initial_count})'async def test_no_leaked_threads():"""Test that no threads remain after agent shutdown."""# Get initial threads (excluding main and asyncio)- initial_threads = [t for t in threading.enumerate()- if t.name not in ['MainThread', 'asyncio_0']]+ initial_threads = [t for t in threading.enumerate() if t.name not in ['MainThread', 'asyncio_0']]initial_count = len(initial_threads)-- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False- )- )++ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))await browser_session.start()-+mock_llm = create_mock_llm()-+agent = Agent(- task="Test task",+ task='Test task',llm=mock_llm,browser_session=browser_session,)-+await agent.run(max_steps=1)-+# Wait briefly for cleanupawait asyncio.sleep(1)-+# Get final threads- final_threads = [t for t in threading.enumerate()- if t.name not in ['MainThread', 'asyncio_0']]+ final_threads = [t for t in threading.enumerate() if t.name not in ['MainThread', 'asyncio_0']]final_count = len(final_threads)-+# No new threads should remain- assert final_count <= initial_count, f"New threads remain: {final_count} (initial: {initial_count})"+ assert final_count <= initial_count, f'New threads remain: {final_count} (initial: {initial_count})'async def test_multiple_agents_cleanup():"""Test cleanup with multiple agents run sequentially."""exit_times = []-+for i in range(3):- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False- )- )+ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))await browser_session.start()-+mock_llm = create_mock_llm()-+agent = Agent(- task=f"Test task {i+1}",+ task=f'Test task {i + 1}',llm=mock_llm,browser_session=browser_session,)-+start_time = time.time()result = await agent.run(max_steps=1)exit_time = time.time() - start_time-- assert result.is_done(), f"Agent {i+1} should have completed"++ assert result.is_done(), f'Agent {i + 1} should have completed'exit_times.append(exit_time)-+# Small delay between agentsawait asyncio.sleep(0.5)-+# All agents should exit quicklyfor i, exit_time in enumerate(exit_times):- assert exit_time < 10, f"Agent {i+1} took {exit_time:.2f}s to exit"-+ assert exit_time < 10, f'Agent {i + 1} took {exit_time:.2f}s to exit'+# Check final statetasks = asyncio.all_tasks(asyncio.get_event_loop())- threads = [t for t in threading.enumerate()- if t.name not in ['MainThread', 'asyncio_0']]-+ threads = [t for t in threading.enumerate() if t.name not in ['MainThread', 'asyncio_0']]+# Should be minimal tasks and no extra threads- assert len(tasks) <= 3, f"Too many tasks remain: {len(tasks)}"- assert len(threads) == 0, f"Extra threads remain: {len(threads)}"+ assert len(tasks) <= 3, f'Too many tasks remain: {len(tasks)}'+ assert len(threads) == 0, f'Extra threads remain: {len(threads)}'async def test_browser_session_stop_method():@@ -163,23 +141,23 @@ async def test_browser_session_stop_method():browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True,- keep_alive=True # Keep alive so we can test stop()+ keep_alive=True, # Keep alive so we can test stop()))await browser_session.start()-+# Verify browser is connected- assert browser_session._cdp_client_root is not None, "Browser should be connected"-+ assert browser_session._cdp_client_root is not None, 'Browser should be connected'+# Call stop() - should clear event buses but keep browser aliveawait browser_session.stop()-+# Browser should still be connected (since we didn't kill it)# But event bus should be fresh- assert browser_session.event_bus is not None, "Should have fresh event bus"-+ assert browser_session.event_bus is not None, 'Should have fresh event bus'+# Now kill to clean upawait browser_session.kill()-+# After kill, browser should be disconnected- assert browser_session._cdp_client_root is None, "Browser should be disconnected after kill"\ No newline at end of file+ assert browser_session._cdp_client_root is None, 'Browser should be disconnected after kill'