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
remove dead code, cleanup examples and tests
Nick Sweetingf19adf9bb38b
diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.pyindex 7915ace8c..20ab422d6 100644--- a/browser_use/browser/profile.py+++ b/browser_use/browser/profile.py@@ -416,7 +416,7 @@ class BrowserLaunchArgs(BaseModel):)executable_path: str | Path | None = Field(default=None,- validation_alias=AliasChoices('chrome_binary_path', 'browser_binary_path'),+ validation_alias=AliasChoices('chrome_binary_path', 'executable_path'),description='Path to the chromium-based browser executable to use.',)headless: bool | None = Field(default=None, description='Whether to run the browser in headless or windowed mode.')diff --git a/codebeaver.yml b/codebeaver.ymldeleted file mode 100644index 560884ba6..000000000--- a/codebeaver.yml+++ /dev/null@@ -1,4 +0,0 @@-environment:-- OPENAI_API_KEY=empty-- AZURE_OPENAI_KEY=empty-from: pytestdiff --git a/debug_pydantic.py b/debug_pydantic.pydeleted file mode 100644index 5be286300..000000000--- a/debug_pydantic.py+++ /dev/null@@ -1,34 +0,0 @@-import inspect--from pydantic import BaseModel--from browser_use.controller.views import ClickElementAction---# Check the pydantic detection logic-def click_element_by_index(params: ClickElementAction, browser_session):- pass---sig = inspect.signature(click_element_by_index)-parameters = list(sig.parameters.values())-parameter_names = [param.name for param in parameters]--print('Parameters:', parameter_names)-print('First param name:', parameters[0].name)-print('First param annotation:', parameters[0].annotation)-print('Is BaseModel:', issubclass(parameters[0].annotation, BaseModel))--# Check the name detection logic-name_check = parameters[0].name in ['params', 'param', 'model'] or parameters[0].name.endswith('_model')-print('Name check passed:', name_check)--is_pydantic = (- parameters- and len(parameters) > 0- and hasattr(parameters[0], 'annotation')- and parameters[0].annotation != parameters[0].empty- and issubclass(parameters[0].annotation, BaseModel)- and name_check-)-print('Is pydantic:', is_pydantic)diff --git a/examples/browser/real_browser.py b/examples/browser/real_browser.pyindex e8ff9ea96..276f9591f 100644--- a/examples/browser/real_browser.py+++ b/examples/browser/real_browser.py@@ -15,14 +15,16 @@ from browser_use.browser import BrowserProfile, BrowserSessionbrowser_profile = BrowserProfile(# NOTE: you need to close your chrome browser - so that this can open your browser in debug mode- browser_binary_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',+ executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',+ user_data_dir='~/.config/browseruse/profiles/default',+ headless=False,)browser_session = BrowserSession(browser_profile=browser_profile)async def main():agent = Agent(- task='In docs.google.com write my Papa a quick letter',+ task='Find todays DOW stock price',llm=ChatOpenAI(model='gpt-4o'),browser_session=browser_session,)diff --git a/examples/custom-functions/file_upload.py b/examples/custom-functions/file_upload.pyindex 4301ca3db..65dd8a5fa 100644--- a/examples/custom-functions/file_upload.py+++ b/examples/custom-functions/file_upload.py@@ -22,7 +22,7 @@ logger = logging.getLogger(__name__)# Initialize browser and controllerbrowser_profile = BrowserProfile(headless=False,- browser_binary_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',+ executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',)controller = Controller()diff --git a/examples/features/cross_origin_iframes.py b/examples/features/cross_origin_iframes.pyindex 3f8c40069..0d59ec418 100644--- a/examples/features/cross_origin_iframes.py+++ b/examples/features/cross_origin_iframes.py@@ -24,7 +24,7 @@ if not os.getenv('OPENAI_API_KEY'):browser_profile = BrowserProfile(- browser_binary_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',+ executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',)browser_session = BrowserSession(browser_profile=browser_profile)controller = Controller()diff --git a/examples/models/bedrock_claude.py b/examples/models/bedrock_claude.pyindex ff03c8f94..468e60108 100644--- a/examples/models/bedrock_claude.py+++ b/examples/models/bedrock_claude.py@@ -53,7 +53,7 @@ args = parser.parse_args()llm = get_llm()browser_profile = BrowserProfile(- # browser_binary_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',+ # executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',)browser_session = BrowserSession(browser_profile=browser_profile)diff --git a/examples/use-cases/post-twitter.py b/examples/use-cases/post-twitter.pyindex c2dda2b5f..3472d4712 100644--- a/examples/use-cases/post-twitter.py+++ b/examples/use-cases/post-twitter.py@@ -70,7 +70,7 @@ def create_twitter_agent(config: TwitterConfig) -> Agent:browser_profile = BrowserProfile(headless=config.headless,- browser_binary_path=config.chrome_path,+ executable_path=config.chrome_path,)browser_session = BrowserSession(browser_profile=browser_profile)diff --git a/tests/extraction_test.py b/tests/extraction_test.pyindex d7549aaad..250dbd191 100644--- a/tests/extraction_test.py+++ b/tests/extraction_test.py@@ -56,7 +56,7 @@ async def test_focus_vs_all_elements():browser = Browser(config=BrowserConfig(- # browser_binary_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',+ # executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',))context = BrowserContext(browser=browser, config=config)diff --git a/tests/test_react_dropdown.py b/tests/test_react_dropdown.pyindex c7dd057a5..d0648476e 100644--- a/tests/test_react_dropdown.py+++ b/tests/test_react_dropdown.py@@ -7,8 +7,7 @@ Simple try of the agent.import osimport sys-from browser_use.browser.browser import Browser, BrowserConfig-from browser_use.browser.context import BrowserContext+from browser_use.browser import BrowserProfile, BrowserSessionsys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))import asyncio@@ -18,27 +17,28 @@ from langchain_openai import ChatOpenAIfrom browser_use import Agent, AgentHistoryListllm = ChatOpenAI(model='gpt-4o')-# browser = Browser(config=BrowserConfig(headless=False))++browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True))agent = Agent(task=('go to https://codepen.io/shyam-king/pen/ByBJoOv and select "Tiger" dropdown and read the text given in "Selected Animal" box (it can be empty as well)'),llm=llm,- browser_context=BrowserContext(- browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),- ),+ browser_session=browser_session,)async def test_dropdown():- history: AgentHistoryList = await agent.run(10)- # await controller.browser.close(force=True)-- result = history.final_result()- assert result is not None- print('result: ', result)- # await browser.close()+ await browser_session.start()+ try:+ history: AgentHistoryList = await agent.run(10)++ result = history.final_result()+ assert result is not None+ print('result: ', result)+ finally:+ await browser_session.stop()if __name__ == '__main__':diff --git a/tests/test_save_conversation.py b/tests/test_save_conversation.pyindex 8b2b25856..94b6d1c1c 100644--- a/tests/test_save_conversation.py+++ b/tests/test_save_conversation.py@@ -8,8 +8,7 @@ import osimport shutilimport sys-from browser_use.browser.browser import Browser, BrowserConfig-from browser_use.browser.context import BrowserContext+from browser_use.browser import BrowserProfile, BrowserSessionsys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))@@ -24,60 +23,69 @@ async def test_save_conversation_contains_slash():if os.path.exists('./logs'):shutil.rmtree('./logs')- agent = Agent(- task=('go to google.com and search for text "hi there"'),- llm=llm,- browser_context=BrowserContext(- browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),- ),- save_conversation_path='logs/conversation',- )- history: AgentHistoryList = await agent.run(20)+ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True))+ await browser_session.start()+ try:+ agent = Agent(+ task=('go to google.com and search for text "hi there"'),+ llm=llm,+ browser_session=browser_session,+ save_conversation_path='logs/conversation',+ )+ history: AgentHistoryList = await agent.run(20)- result = history.final_result()- assert result is not None+ result = history.final_result()+ assert result is not None- assert os.path.exists('./logs'), 'logs directory was not created'- assert os.path.exists('./logs/conversation_2.txt'), 'logs file was not created'+ assert os.path.exists('./logs'), 'logs directory was not created'+ assert os.path.exists('./logs/conversation_2.txt'), 'logs file was not created'+ finally:+ await browser_session.stop()async def test_save_conversation_not_contains_slash():if os.path.exists('./logs'):shutil.rmtree('./logs')- agent = Agent(- task=('go to google.com and search for text "hi there"'),- llm=llm,- browser_context=BrowserContext(- browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),- ),- save_conversation_path='logs',- )- history: AgentHistoryList = await agent.run(20)+ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True))+ await browser_session.start()+ try:+ agent = Agent(+ task=('go to google.com and search for text "hi there"'),+ llm=llm,+ browser_session=browser_session,+ save_conversation_path='logs',+ )+ history: AgentHistoryList = await agent.run(20)- result = history.final_result()- assert result is not None+ result = history.final_result()+ assert result is not None- assert os.path.exists('./logs'), 'logs directory was not created'- assert os.path.exists('./logs/_2.txt'), 'logs file was not created'+ assert os.path.exists('./logs'), 'logs directory was not created'+ assert os.path.exists('./logs/_2.txt'), 'logs file was not created'+ finally:+ await browser_session.stop()async def test_save_conversation_deep_directory():if os.path.exists('./logs'):shutil.rmtree('./logs')- agent = Agent(- task=('go to google.com and search for text "hi there"'),- llm=llm,- browser_context=BrowserContext(- browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),- ),- save_conversation_path='logs/deep/directory/conversation',- )- history: AgentHistoryList = await agent.run(20)-- result = history.final_result()- assert result is not None-- assert os.path.exists('./logs/deep/directory'), 'logs directory was not created'- assert os.path.exists('./logs/deep/directory/conversation_2.txt'), 'logs file was not created'+ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True))+ await browser_session.start()+ try:+ agent = Agent(+ task=('go to google.com and search for text "hi there"'),+ llm=llm,+ browser_session=browser_session,+ save_conversation_path='logs/deep/directory/conversation',+ )+ history: AgentHistoryList = await agent.run(20)++ result = history.final_result()+ assert result is not None++ assert os.path.exists('./logs/deep/directory'), 'logs directory was not created'+ assert os.path.exists('./logs/deep/directory/conversation_2.txt'), 'logs file was not created'+ finally:+ await browser_session.stop()diff --git a/tests/test_self_registered_actions.py b/tests/test_self_registered_actions.pyindex 2221f7866..22ace11b2 100644--- a/tests/test_self_registered_actions.py+++ b/tests/test_self_registered_actions.py@@ -1,4 +1,3 @@-import asyncioimport osimport pytest@@ -7,32 +6,20 @@ from pydantic import BaseModel, SecretStrfrom browser_use.agent.service import Agentfrom browser_use.agent.views import AgentHistoryList-from browser_use.browser.browser import Browser, BrowserConfig+from browser_use.browser import BrowserProfile, BrowserSessionfrom browser_use.controller.service import Controller[email protected](scope='session')-def event_loop():- loop = asyncio.get_event_loop_policy().new_event_loop()- yield loop- loop.close()--[email protected](scope='session')-async def browser(event_loop):- browser_instance = Browser(- config=BrowserConfig(+async def browser_session():+ browser_session = BrowserSession(+ browser_profile=BrowserProfile(headless=True,))- yield browser_instance- await browser_instance.close()---async def context(browser):- async with await browser.new_context() as context:- yield context+ await browser_session.start()+ yield browser_session+ await browser_session.stop()@pytest.fixture@@ -101,7 +88,6 @@ def llm():# @pytest.mark.skip(reason="Skipping test for now")async def test_self_registered_actions_no_pydantic(llm, controller):"""Test self-registered actions with individual arguments"""agent = Agent(@@ -119,7 +105,6 @@ async def test_self_registered_actions_no_pydantic(llm, controller):# @pytest.mark.skip(reason="Skipping test for now")async def test_mixed_arguments_actions(llm, controller):"""Test actions with mixed argument types"""@@ -150,7 +135,6 @@ async def test_mixed_arguments_actions(llm, controller):pytest.fail(f'{correct} not found in extracted content')async def test_pydantic_simple_model(llm, controller):"""Test action with a simple Pydantic model argument"""agent = Agent(@@ -172,7 +156,6 @@ async def test_pydantic_simple_model(llm, controller):pytest.fail(f'{correct} not found in extracted content')async def test_pydantic_nested_model(llm, controller):"""Test action with a nested Pydantic model argument"""agent = Agent(