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
add new param optimization test
Nick Sweetingaddeb56f8c4a
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 5cba9cd43..7cea06b22 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -780,7 +780,7 @@ class Controller(Generic[Context]):async def read_cell_contents(cell_or_range: str, browser_session: BrowserSession):page = await browser_session.get_current_page()- await select_cell_or_range(browser_session=browser_session, cell_or_range=cell_or_range)+ await select_cell_or_range(cell_or_range=cell_or_range, page=page)await page.keyboard.press('ControlOrMeta+C')await asyncio.sleep(0.1)@@ -793,7 +793,7 @@ class Controller(Generic[Context]):async def update_cell_contents(cell_or_range: str, new_contents_tsv: str, browser_session: BrowserSession):page = await browser_session.get_current_page()- await select_cell_or_range(browser_session=browser_session, cell_or_range=cell_or_range)+ await select_cell_or_range(cell_or_range=cell_or_range, page=page)# simulate paste event from clipboard with TSV contentawait page.evaluate(f"""@@ -808,7 +808,7 @@ class Controller(Generic[Context]):async def clear_cell_contents(cell_or_range: str, browser_session: BrowserSession):page = await browser_session.get_current_page()- await select_cell_or_range(browser_session=browser_session, cell_or_range=cell_or_range)+ await select_cell_or_range(cell_or_range=cell_or_range, page=page)await page.keyboard.press('Backspace')return ActionResult(extracted_content=f'Cleared cells: {cell_or_range}', include_in_memory=False)diff --git a/examples/custom-functions/action_filters.py b/examples/custom-functions/action_filters.pyindex 216336594..8d327affb 100644--- a/examples/custom-functions/action_filters.py+++ b/examples/custom-functions/action_filters.py@@ -40,8 +40,7 @@ registry = controller.registry# Action will only be available to Agent on Google domains because of the domain filter@registry.action(description='Trigger disco mode', domains=['google.com', '*.google.com'])-async def disco_mode(browser_session: BrowserSession):- page = await browser_session.get_current_page()+async def disco_mode(page: Page):await page.evaluate("""() => {// define the wiggle animationdocument.styleSheets[0].insertRule('@keyframes wiggle { 0% { transform: rotate(0deg); } 50% { transform: rotate(10deg); } 100% { transform: rotate(0deg); } }');@@ -59,9 +58,8 @@ def is_login_page(page: Page) -> bool:# then use it in the action decorator to limit the action to only be available on pages where the filter returns True@registry.action(description='Use the force, luke', page_filter=is_login_page)-async def use_the_force(browser_session: BrowserSession):+async def use_the_force(page: Page):# this will only ever run on pages that matched the filter- page = await browser_session.get_current_page()assert is_login_page(page)await page.evaluate("""() => { document.querySelector('body').innerHTML = 'These are not the droids you are looking for';}""")diff --git a/examples/custom-functions/clipboard.py b/examples/custom-functions/clipboard.pyindex e5c2e4b44..d1f520366 100644--- a/examples/custom-functions/clipboard.py+++ b/examples/custom-functions/clipboard.py@@ -10,6 +10,7 @@ load_dotenv()import pyperclipfrom langchain_openai import ChatOpenAI+from playwright.async_api import Pagefrom browser_use import Agent, Controllerfrom browser_use.agent.views import ActionResult@@ -28,10 +29,9 @@ def copy_to_clipboard(text: str):@controller.registry.action('Paste text from clipboard')-async def paste_from_clipboard(browser_session: BrowserSession):+async def paste_from_clipboard(page: Page):text = pyperclip.paste()# send text to browser- page = await browser_session.get_current_page()await page.keyboard.type(text)return ActionResult(extracted_content=text)diff --git a/tests/ci/test_action_registry.py b/tests/ci/test_action_registry.pyindex 24f2eae56..195807431 100644--- a/tests/ci/test_action_registry.py+++ b/tests/ci/test_action_registry.py@@ -312,6 +312,49 @@ class TestActionRegistryParameterPatterns:assert 'Legacy context: test2, URL:' in result2.extracted_contentassert '/test' in result2.extracted_content+ @pytest.mark.asyncio+ async def test_page_parameter_optimization(self, test_browser: BrowserSession, httpserver: HTTPServer):+ """Test that actions can use page: Page parameter directly instead of browser_session"""+ registry = Registry()++ httpserver.expect_request('/test').respond_with_data('<html><body>Test Page</body></html>')+ page = await test_browser.get_current_page()+ await page.goto(httpserver.url_for('/test'))++ # Action that takes page directly (optimized pattern)+ @registry.action('Action with direct page parameter')+ async def direct_page_action(text: str, page: Page):+ # This is the optimized pattern - no need to call get_current_page()+ return ActionResult(extracted_content=f'Direct page: {text}, URL: {page.url}')++ # Action that takes browser_session and calls get_current_page (old pattern)+ @registry.action('Action with browser_session parameter')+ async def browser_session_action(text: str, browser_session: BrowserSession):+ page = await browser_session.get_current_page()+ return ActionResult(extracted_content=f'Browser session: {text}, URL: {page.url}')++ # Test direct page parameter+ result1 = await registry.execute_action('direct_page_action', {'text': 'optimized'}, browser_session=test_browser)+ assert 'Direct page: optimized, URL:' in result1.extracted_content+ assert '/test' in result1.extracted_content++ # Test browser_session parameter (should still work)+ result2 = await registry.execute_action('browser_session_action', {'text': 'legacy'}, browser_session=test_browser)+ assert 'Browser session: legacy, URL:' in result2.extracted_content+ assert '/test' in result2.extracted_content++ # Verify both patterns work with pydantic models too+ class PageActionParams(BaseActionModel):+ message: str = Field(..., description='Test message')++ @registry.action('Pydantic action with page', param_model=PageActionParams)+ async def pydantic_page_action(params: PageActionParams, page: Page):+ return ActionResult(extracted_content=f'Pydantic page: {params.message}, URL: {page.url}')++ result3 = await registry.execute_action('pydantic_page_action', {'message': 'pydantic'}, browser_session=test_browser)+ assert 'Pydantic page: pydantic, URL:' in result3.extracted_content+ assert '/test' in result3.extracted_content+class TestActionToActionCalling:"""Test scenarios where actions call other actions"""