Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

updating more browser examples

Nick Sweeting7939f4522a0e
diff --git a/examples/browser/real_browser.py b/examples/browser/real_browser.py
index 1cffb99bb..e8ff9ea96 100644
--- a/examples/browser/real_browser.py
+++ b/examples/browser/real_browser.py
@@ -10,25 +10,25 @@ load_dotenv()
from langchain_openai import ChatOpenAI
-from browser_use import Agent, Browser, BrowserConfig
+from browser_use import Agent
+from browser_use.browser import BrowserProfile, BrowserSession
-browser = Browser(
- config=BrowserConfig(
- # 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',
- )
+browser_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',
)
+browser_session = BrowserSession(browser_profile=browser_profile)
async def main():
agent = Agent(
task='In docs.google.com write my Papa a quick letter',
llm=ChatOpenAI(model='gpt-4o'),
- browser=browser,
+ browser_session=browser_session,
)
await agent.run()
- await browser.close()
+ await browser_session.close()
input('Press Enter to close...')
diff --git a/examples/browser/using_cdp.py b/examples/browser/using_cdp.py
index deb8db74e..71538699b 100644
--- a/examples/browser/using_cdp.py
+++ b/examples/browser/using_cdp.py
@@ -25,18 +25,17 @@ from langchain_google_genai import ChatGoogleGenerativeAI
from pydantic import SecretStr
from browser_use import Agent, Controller
-from browser_use.browser.browser import Browser, BrowserConfig
+from browser_use.browser import BrowserProfile, BrowserSession
api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
raise ValueError('GOOGLE_API_KEY is not set')
-browser = Browser(
- config=BrowserConfig(
- headless=False,
- cdp_url='http://localhost:9222',
- )
+browser_profile = BrowserProfile(
+ headless=False,
+ cdp_url='http://localhost:9222',
)
+browser_session = BrowserSession(browser_profile=browser_profile)
controller = Controller()
@@ -48,11 +47,11 @@ async def main():
task=task,
llm=model,
controller=controller,
- browser=browser,
+ browser_session=browser_session,
)
await agent.run()
- await browser.close()
+ await browser_session.close()
input('Press Enter to close...')
diff --git a/examples/browser/window_sizing.py b/examples/browser/window_sizing.py
index 51c7b279f..6b92d109d 100644
--- a/examples/browser/window_sizing.py
+++ b/examples/browser/window_sizing.py
@@ -10,6 +10,10 @@ This example shows how to:
import asyncio
from typing import Any
+from dotenv import load_dotenv
+
+load_dotenv()
+
from browser_use.browser import BrowserProfile, BrowserSession
diff --git a/examples/ui/streamlit_demo.py b/examples/ui/streamlit_demo.py
index 1c577d0d4..d18e11a11 100644
--- a/examples/ui/streamlit_demo.py
+++ b/examples/ui/streamlit_demo.py
@@ -18,7 +18,7 @@ load_dotenv()
import streamlit as st
from browser_use import Agent
-from browser_use.browser.browser import Browser, BrowserConfig
+from browser_use.browser import BrowserSession
from browser_use.controller.service import Controller
if os.name == 'nt':
@@ -54,16 +54,16 @@ def get_llm(provider: str):
def initialize_agent(query: str, provider: str):
llm = get_llm(provider)
controller = Controller()
- browser = Browser(config=BrowserConfig())
+ browser_session = BrowserSession()
return Agent(
task=query,
llm=llm,
controller=controller,
- browser=browser,
+ browser_session=browser_session,
use_vision=True,
max_actions_per_step=1,
- ), browser
+ ), browser_session
# Streamlit UI
@@ -74,7 +74,7 @@ provider = st.radio('Select LLM Provider:', ['openai', 'anthropic'], index=0)
if st.button('Run Agent'):
st.write('Initializing agent...')
- agent, browser = initialize_agent(query, provider)
+ agent, browser_session = initialize_agent(query, provider)
async def run_agent():
with st.spinner('Running automation...'):
@@ -83,4 +83,4 @@ if st.button('Run Agent'):
asyncio.run(run_agent())
- st.button('Close Browser', on_click=lambda: asyncio.run(browser.close()))
+ st.button('Close Browser', on_click=lambda: asyncio.run(browser_session.close()))
diff --git a/examples/use-cases/online_coding_agent.py b/examples/use-cases/online_coding_agent.py
index 2bac3206f..bb5783ab5 100644
--- a/examples/use-cases/online_coding_agent.py
+++ b/examples/use-cases/online_coding_agent.py
@@ -12,37 +12,37 @@ load_dotenv()
from langchain_openai import ChatOpenAI
-from browser_use import Agent, Browser
+from browser_use import Agent
+from browser_use.browser import BrowserSession
if not os.getenv('OPENAI_API_KEY'):
raise ValueError('OPENAI_API_KEY is not set. Please add it to your environment variables.')
async def main():
- browser = Browser()
- async with await browser.new_context() as context:
- model = ChatOpenAI(model='gpt-4o')
-
- # Initialize browser agent
- agent1 = Agent(
- task='Open an online code editor programiz.',
- llm=model,
- browser_context=context,
- )
- executor = Agent(
- task='Executor. Execute the code written by the coder and suggest some updates if there are errors.',
- llm=model,
- browser_context=context,
- )
-
- coder = Agent(
- task='Coder. Your job is to write and complete code. You are an expert coder. Code a simple calculator. Write the code on the coding interface after agent1 has opened the link.',
- llm=model,
- browser_context=context,
- )
- await agent1.run()
- await executor.run()
- await coder.run()
+ browser_session = BrowserSession()
+ model = ChatOpenAI(model='gpt-4o')
+
+ # Initialize browser agent
+ agent1 = Agent(
+ task='Open an online code editor programiz.',
+ llm=model,
+ browser_session=browser_session,
+ )
+ executor = Agent(
+ task='Executor. Execute the code written by the coder and suggest some updates if there are errors.',
+ llm=model,
+ browser_session=browser_session,
+ )
+
+ coder = Agent(
+ task='Coder. Your job is to write and complete code. You are an expert coder. Code a simple calculator. Write the code on the coding interface after agent1 has opened the link.',
+ llm=model,
+ browser_session=browser_session,
+ )
+ await agent1.run()
+ await executor.run()
+ await coder.run()
if __name__ == '__main__':
diff --git a/examples/use-cases/post-twitter.py b/examples/use-cases/post-twitter.py
index 956b2dd2b..c2dda2b5f 100644
--- a/examples/use-cases/post-twitter.py
+++ b/examples/use-cases/post-twitter.py
@@ -33,7 +33,7 @@ load_dotenv()
from langchain_openai import ChatOpenAI
from browser_use import Agent, Controller
-from browser_use.browser.browser import Browser, BrowserConfig
+from browser_use.browser import BrowserProfile, BrowserSession
if not os.getenv('OPENAI_API_KEY'):
raise ValueError('OPENAI_API_KEY is not set. Please add it to your environment variables.')
@@ -68,12 +68,11 @@ config = TwitterConfig(
def create_twitter_agent(config: TwitterConfig) -> Agent:
llm = ChatOpenAI(model=config.model, api_key=config.openai_api_key)
- browser = Browser(
- config=BrowserConfig(
- headless=config.headless,
- browser_binary_path=config.chrome_path,
- )
+ browser_profile = BrowserProfile(
+ headless=config.headless,
+ browser_binary_path=config.chrome_path,
)
+ browser_session = BrowserSession(browser_profile=browser_profile)
controller = Controller()
@@ -105,7 +104,7 @@ def create_twitter_agent(config: TwitterConfig) -> Agent:
""",
llm=llm,
controller=controller,
- browser=browser,
+ browser_session=browser_session,
)
diff --git a/examples/use-cases/scrolling_page.py b/examples/use-cases/scrolling_page.py
index b8417445e..3ecbac438 100644
--- a/examples/use-cases/scrolling_page.py
+++ b/examples/use-cases/scrolling_page.py
@@ -13,7 +13,7 @@ load_dotenv()
from langchain_openai import ChatOpenAI
from browser_use import Agent
-from browser_use.browser.browser import Browser, BrowserConfig
+from browser_use.browser import BrowserProfile, BrowserSession
if not os.getenv('OPENAI_API_KEY'):
raise ValueError('OPENAI_API_KEY is not set')
@@ -27,11 +27,14 @@ If no amount is specified, the agent will scroll down by one page height.
llm = ChatOpenAI(model='gpt-4o')
+browser_profile = BrowserProfile(headless=False)
+browser_session = BrowserSession(browser_profile=browser_profile)
+
agent = Agent(
# task="Navigate to 'https://en.wikipedia.org/wiki/Internet' and scroll down by one page - then scroll up by 100 pixels - then scroll down by 100 pixels - then scroll down by 10000 pixels.",
task="Navigate to 'https://en.wikipedia.org/wiki/Internet' and scroll to the string 'The vast majority of computer'",
llm=llm,
- browser=Browser(config=BrowserConfig(headless=False)),
+ browser_session=browser_session,
)
diff --git a/examples/use-cases/shopping.py b/examples/use-cases/shopping.py
index e83563962..a0cf985b8 100644
--- a/examples/use-cases/shopping.py
+++ b/examples/use-cases/shopping.py
@@ -10,7 +10,8 @@ load_dotenv()
from langchain_openai import ChatOpenAI
-from browser_use import Agent, Browser
+from browser_use import Agent
+from browser_use.browser import BrowserSession
task = """
### Prompt for Shopping Agent – Migros Online Grocery Order
@@ -109,19 +110,19 @@ At this stage, check the basket on the top right (indicates the price) and check
**Important:** Ensure efficiency and accuracy throughout the process."""
-browser = Browser()
+browser_session = BrowserSession()
agent = Agent(
task=task,
llm=ChatOpenAI(model='gpt-4o'),
- browser=browser,
+ browser_session=browser_session,
)
async def main():
await agent.run()
input('Press Enter to close the browser...')
- await browser.close()
+ await browser_session.close()
if __name__ == '__main__':