Skip to content

Basic Usage Examples

This page demonstrates common tasks you can perform with Browser-Use through practical examples. Each example includes the complete code you need to get started.

Example 1: Searching the Web

This example shows how to create an agent that searches for information on a specific topic and summarizes the results.

python
import asyncio
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from browser_use import Agent

load_dotenv()

async def search_and_summarize():
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    
    agent = Agent(
        task="Search for 'latest advancements in quantum computing' and provide a summary of the 3 most significant breakthroughs from this year.",
        llm=llm
    )
    
    result = await agent.run()
    print(result)

if __name__ == "__main__":
    asyncio.run(search_and_summarize())

Example 2: Filling Out Forms

This example demonstrates how to fill out a web form, such as a contact or registration form.

python
import asyncio
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from browser_use import Agent

load_dotenv()

async def fill_contact_form():
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    
    agent = Agent(
        task="""Go to https://example.com/contact and fill out the contact form with the following information:
        - Name: John Smith
        - Email: [email protected]
        - Subject: Product Inquiry
        - Message: I'm interested in learning more about your enterprise solutions. Could you please send me more information?
        
        After filling the form, take a screenshot before submitting (but don't actually submit the form).""",
        llm=llm
    )
    
    result = await agent.run()
    print(result)

if __name__ == "__main__":
    asyncio.run(fill_contact_form())

Example 3: Data Extraction

This example shows how to extract structured data from a website, such as product information from an e-commerce site.

python
import asyncio
import json
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from browser_use import Agent

load_dotenv()

async def extract_product_data():
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    
    agent = Agent(
        task="""Go to an online bookstore (like books.toscrape.com) and extract the following information for the top 5 bestselling books:
        - Title
        - Author
        - Price
        - Rating
        - Category
        
        Format the results as a JSON object.""",
        llm=llm
    )
    
    result = await agent.run()
    
    # The agent should return JSON-formatted text, which we can parse
    try:
        # Find JSON in the response - often the agent might include explanatory text
        import re
        json_pattern = r'\{[\s\S]*\}'
        json_match = re.search(json_pattern, result)
        if json_match:
            data = json.loads(json_match.group())
            print(json.dumps(data, indent=2))
        else:
            print(result)
    except json.JSONDecodeError:
        print("Could not parse JSON from result:")
        print(result)

if __name__ == "__main__":
    asyncio.run(extract_product_data())

Example 4: Multi-Page Navigation

This example demonstrates how to navigate through multiple pages of a website to gather comprehensive information.

python
import asyncio
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from browser_use import Agent, Memory

load_dotenv()

async def multi_page_navigation():
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    
    # Using memory to retain information across page navigations
    agent = Agent(
        task="""Research the top 3 tourist attractions in Paris:
        1. Find and visit the official website or Wikipedia page for each attraction
        2. For each attraction, gather: description, visiting hours, ticket prices, and one interesting fact
        3. Compile the information into a well-structured summary""",
        llm=llm,
        memory=Memory()  # Enable memory to remember information between pages
    )
    
    result = await agent.run()
    print(result)

if __name__ == "__main__":
    asyncio.run(multi_page_navigation())

Example 5: Monitoring a Website

This example shows how to monitor a website for specific content changes.

python
import asyncio
import time
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from browser_use import Agent

load_dotenv()

async def monitor_website():
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    
    print("Starting website monitoring...")
    
    # Set to store previous headlines to detect changes
    previous_headlines = set()
    first_run = True
    
    while True:
        agent = Agent(
            task="""Go to a news website (like example.com/news) and extract all headline titles 
            from the main page. Return them as a numbered list.""",
            llm=llm
        )
        
        result = await agent.run()
        
        # Extract headlines from the result
        import re
        headlines = set(re.findall(r'\d+\.\s(.+)', result))
        
        if first_run:
            print("Initial headlines detected:")
            for headline in headlines:
                print(f"- {headline}")
            first_run = False
        else:
            # Find new headlines
            new_headlines = headlines - previous_headlines
            if new_headlines:
                print("\nNew headlines detected:")
                for headline in new_headlines:
                    print(f"- {headline}")
        
        previous_headlines = headlines
        
        # Wait before checking again (e.g., every 10 minutes)
        print("\nWaiting for 10 minutes before checking again...")
        # In a real application, you would wait longer
        # For demonstration, you can use a shorter interval
        await asyncio.sleep(10)  # Just waiting 10 seconds for demo purposes
        # In production: time.sleep(600)  # 10 minutes
        
        # To stop the demo after one check
        break

if __name__ == "__main__":
    asyncio.run(monitor_website())

Next Steps

Now that you've seen these basic examples, you can: