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
vastly improve examples/browser/multiple_agents_same_browser.py
Nick Sweetingde17b02f4a07
diff --git a/examples/browser/multiple_agents_same_browser.py b/examples/browser/multiple_agents_same_browser.pyindex a7ff00532..6cfe9492e 100644--- a/examples/browser/multiple_agents_same_browser.py+++ b/examples/browser/multiple_agents_same_browser.py@@ -2,72 +2,47 @@ import asyncioimport osimport sys-from langchain_openai import ChatOpenAI--from browser_use.browser.session import BrowserSession-sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))from dotenv import load_dotenv+from browser_use.browser.session import BrowserSession+load_dotenv()+from langchain_openai import ChatOpenAI+from browser_use import Agent-# Video: https://preview.screen.studio/share/8Elaq9smasync def main():- # Persist the browser state across agentsbrowser_session = BrowserSession(keep_alive=True,user_data_dir=None,+ headless=False,)await browser_session.start()- async def get_input():- return await asyncio.get_event_loop().run_in_executor(- None, lambda: input('Enter task (p: pause current agent, r: resume, b: break): ')- )-current_agent = Nonellm = ChatOpenAI(model='gpt-4o')- while True:- task = await get_input()-- if task.lower() == 'p':- # Pause the current agent if one exists- if current_agent:- current_agent.pause()- continue- elif task.lower() == 'r':- # Resume the current agent if one exists- if current_agent:- current_agent.resume()- continue- elif task.lower() == 'b':- # Break the current agent's execution if one exists- if current_agent:- current_agent.stop()- current_agent = None- continue-- # If there's a current agent running, pause it before starting new one- # if current_agent:- # current_agent.pause()+ task1 = 'find todays weather on San Francisco and extract it as json'+ task2 = 'find todays weather in Zurich and extract it as json'- await browser_session.create_new_tab()- current_agent = Agent(- task=task,- browser_session=browser_session,- llm=llm,- )+ agent1 = Agent(+ task=task1,+ browser_session=browser_session,+ llm=llm,+ )+ agent2 = Agent(+ task=task2,+ browser_session=browser_session,+ llm=llm,+ )- # Run the agent asynchronously without blocking- asyncio.create_task(current_agent.run())+ await asyncio.gather(agent1.run(), agent2.run())+ await browser_session.kill()asyncio.run(main())--# Now aad the cheapest to the cart