Browser Use multi-tab support
Quick answer
The
browser-use package supports multi-tab browsing by creating separate Agent instances for each tab, each maintaining its own state and context. Manage multiple tabs by instantiating multiple Agent objects and running them asynchronously or sequentially to simulate multi-tab interactions.PREREQUISITES
Python 3.8+pip install browser-usepip install langchain-openaiOpenAI API key (free tier works)
Setup
Install the browser-use package and langchain-openai for LLM integration. Set your OpenAI API key as an environment variable.
pip install browser-use langchain-openai output
Collecting browser-use Collecting langchain-openai Successfully installed browser-use langchain-openai
Step by step
Create multiple Agent instances to represent different browser tabs. Each agent maintains its own browsing context. Run them asynchronously to simulate multi-tab browsing.
import os
import asyncio
from browser_use import Agent
from langchain_openai import ChatOpenAI
async def main():
# Initialize two separate browser tabs as agents
tab1 = Agent(
task="Search for AI news on google.com",
llm=ChatOpenAI(model="gpt-4o-mini", openai_api_key=os.environ["OPENAI_API_KEY"])
)
tab2 = Agent(
task="Check weather forecast on weather.com",
llm=ChatOpenAI(model="gpt-4o-mini", openai_api_key=os.environ["OPENAI_API_KEY"])
)
# Run both tabs concurrently
results = await asyncio.gather(tab1.run(), tab2.run())
print("Tab 1 result:", results[0])
print("Tab 2 result:", results[1])
if __name__ == "__main__":
asyncio.run(main()) output
Tab 1 result: AI news summary from google.com Tab 2 result: Weather forecast details from weather.com
Common variations
- Use synchronous calls by running
agent.run_sync()instead of asyncagent.run(). - Switch to different LLM models by changing
ChatOpenAI(model=...)to other supported models likegpt-4o-mini. - Manage more tabs by creating additional
Agentinstances and running them concurrently.
import os
from browser_use import Agent
from langchain_openai import ChatOpenAI
def main():
tab1 = Agent(
task="Search for AI news",
llm=ChatOpenAI(model="gpt-4o-mini", openai_api_key=os.environ["OPENAI_API_KEY"])
)
tab2 = Agent(
task="Check weather forecast",
llm=ChatOpenAI(model="gpt-4o-mini", openai_api_key=os.environ["OPENAI_API_KEY"])
)
result1 = tab1.run_sync()
result2 = tab2.run_sync()
print("Tab 1 result:", result1)
print("Tab 2 result:", result2)
if __name__ == "__main__":
main() output
Tab 1 result: AI news summary Tab 2 result: Weather forecast details
Troubleshooting
- If you see
ModuleNotFoundError, ensurebrowser-useandlangchain-openaiare installed. - If
OPENAI_API_KEYis missing, set it in your environment:export OPENAI_API_KEY='your_key'. - For concurrency issues, verify you are using
asyncio.run()properly and your Python version supports async.
Key Takeaways
- Use separate
Agentinstances to simulate multiple browser tabs inbrowser-use. - Run agents asynchronously with
asyncio.gather()for concurrent multi-tab browsing. - Set your OpenAI API key in environment variables and install required packages before running.
- Switch between synchronous and asynchronous execution depending on your application needs.