from browser_use import Agent, ChatBrowserUseagent = Agent( task=""" 1. Go to news.ycombinator.com 2. Use extract action with query "first 5 post titles and their URLs" """, llm=ChatBrowserUse(),)result = await agent.run()print(result.final_result())
Always explicitly mention “use extract action” in your task for best results.
from pydantic import BaseModelfrom typing import Listclass Post(BaseModel): title: str url: str points: int num_comments: intclass HackerNewsPosts(BaseModel): posts: List[Post]
2
Pass Schema to Agent
from browser_use import Agent, ChatBrowserUseagent = Agent( task="Go to Hacker News and extract the top 10 posts", llm=ChatBrowserUse(), output_model_schema=HackerNewsPosts,)
3
Parse Results
history = await agent.run()# Get structured outputresult_json = history.final_result()parsed = HackerNewsPosts.model_validate_json(result_json)# Use typed datafor post in parsed.posts: print(f"Title: {post.title}") print(f"URL: {post.url}") print(f"Points: {post.points}") print(f"Comments: {post.num_comments}") print("---")
For large pages, use start_from_char to extract in chunks:
agent = Agent( task=""" 1. Go to long-article.com 2. Extract all product names using extract action 3. If truncated, use start_from_char parameter to continue """, llm=ChatBrowserUse(),)
The agent automatically handles truncation:
First call: extracts 0 to 100,000 chars
If truncated, response includes next_start_char
Agent calls extract again with start_from_char=100000
from browser_use import Agent, ChatBrowserUseagent = Agent( task=""" 1. Go to documentation page 2. Use search_page action to find all occurrences of 'API key' 3. Report the count and locations """, llm=ChatBrowserUse(),)
Parameters:
pattern: Text or regex pattern to search
regex: Set to True for regex patterns
case_sensitive: Case-sensitive matching
context_chars: Characters of context around matches
max_results: Limit results returned
css_scope: Search within specific element
Example with regex:
task="""1. Go to pricing page2. Use search_page with pattern='\$\d+\.\d{2}' and regex=True3. Extract all prices found"""
agent = Agent( task=""" 1. Go to product listing 2. Use find_elements with selector='.product-card' 3. Use attributes=['data-id', 'href'] to get product IDs and links 4. Report the count and data """, llm=ChatBrowserUse(),)
task="""1. Go to links page2. Use find_elements with: - selector='a.external-link' - attributes=['href', 'title'] - include_text=True3. Count and list all external links"""
from browser_use import Agent, ChatBrowserUseagent = Agent( task=""" 1. Go to example-shop.com/products 2. Extract all products on page 1 3. Click 'Next' to go to page 2 4. Extract all products on page 2 5. Continue until page 5 6. Combine all results """, llm=ChatBrowserUse(), output_model_schema=Products, max_steps=50,)
from browser_use import Tools, ActionResult, Agent, ChatBrowserUse, BrowserSessionfrom typing import Listimport jsontools = Tools()all_data = []@tools.action('Save extracted data to collection')async def save_data(items: List[dict]) -> ActionResult: all_data.extend(items) return ActionResult( extracted_content=f"Saved {len(items)} items. Total: {len(all_data)}" )@tools.action('Export all collected data to file')async def export_data() -> ActionResult: with open('extracted_data.json', 'w') as f: json.dump(all_data, f, indent=2) return ActionResult( extracted_content=f"Exported {len(all_data)} items to extracted_data.json", is_done=True )agent = Agent( task=""" 1. Go to products page 2. Extract products on current page and save_data 3. If 'Next' button exists, click it and repeat step 2 4. When no more pages, call export_data """, llm=ChatBrowserUse(), tools=tools,)
from pydantic import BaseModelfrom typing import Listclass TableRow(BaseModel): column1: str column2: str column3: floatclass TableData(BaseModel): headers: List[str] rows: List[TableRow]agent = Agent( task=""" 1. Go to page with data table 2. Extract all rows from the table """, llm=ChatBrowserUse(), output_model_schema=TableData,)
agent = Agent( task=""" 1. Go to page with large table 2. Scroll down to load more rows 3. Repeat scrolling until no new rows appear 4. Extract all visible table data """, llm=ChatBrowserUse(), max_steps=100,)
agent = Agent( task=""" 1. Go to dynamic page 2. Wait for 3 seconds to let content load 3. If loading spinner still visible, wait 3 more seconds 4. Extract data once fully loaded """, llm=ChatBrowserUse(),)
agent = Agent( task=""" 1. Go to infinite scroll page 2. Scroll down 10 pages to load content 3. Extract all loaded items 4. Save results """, llm=ChatBrowserUse(),)