ConnectOnionConnectOnion

Browser Automation

Control web browsers with natural language commands using Playwright

What You'll Learn

Navigate websites and interact with elements
Take screenshots (full page or viewport)
Extract content and scrape data
Control browser with natural language

Quick Start

browser_example.py
1from connectonion import Agent 2from connectonion.tools import BrowserTool 3 4# Initialize browser tool 5browser = BrowserTool() 6 7# Create agent with browser capabilities 8agent = Agent("browser-bot", tools=[browser]) 9 10# Control browser with natural language 11result = agent.input("Navigate to example.com and take a screenshot") 12print(result) 13 14# Extract content 15content = agent.input("Extract all the links from the page") 16print(content) 17 18# Complex interactions 19agent.input("Fill the search box with 'AI agents' and click search")
Python REPL
Interactive
Screenshot saved to example_screenshot.png
[{text: 'About', href: '/about'}, {text: 'Contact', href: '/contact'}]
Search completed successfully

Complete Browser Agent

agent.py
1#!/usr/bin/env python3 2"""Browser automation agent with Playwright integration""" 3 4import os 5from connectonion import Agent 6from playwright.sync_api import sync_playwright 7from typing import Optional, Dict, Any 8 9class BrowserTool: 10 """Tool for browser automation using Playwright""" 11 12 def __init__(self): 13 self.playwright = None 14 self.browser = None 15 self.page = None 16 17 def start_browser(self, headless: bool = False) -> str: 18 """Start a new browser instance""" 19 self.playwright = sync_playwright().start() 20 self.browser = self.playwright.chromium.launch(headless=headless) 21 self.page = self.browser.new_page() 22 return "Browser started successfully" 23 24 def navigate(self, url: str) -> str: 25 """Navigate to a URL""" 26 if not self.page: 27 return "Browser not started. Please start browser first." 28 self.page.goto(url) 29 return f"Navigated to {url}" 30 31 def screenshot(self, filename: str = "screenshot.png", full_page: bool = False) -> str: 32 """Take a screenshot of the current page""" 33 if not self.page: 34 return "Browser not started. Please start browser first." 35 self.page.screenshot(path=filename, full_page=full_page) 36 return f"Screenshot saved to {filename}" 37 38 def extract_text(self, selector: str = "body") -> str: 39 """Extract text content from the page""" 40 if not self.page: 41 return "Browser not started. Please start browser first." 42 element = self.page.query_selector(selector) 43 if element: 44 return element.text_content() 45 return "No content found" 46 47 def click(self, selector: str) -> str: 48 """Click an element on the page""" 49 if not self.page: 50 return "Browser not started. Please start browser first." 51 self.page.click(selector) 52 return f"Clicked element: {selector}" 53 54 def fill(self, selector: str, text: str) -> str: 55 """Fill a form field with text""" 56 if not self.page: 57 return "Browser not started. Please start browser first." 58 self.page.fill(selector, text) 59 return f"Filled {selector} with text" 60 61 def extract_links(self) -> list: 62 """Extract all links from the current page""" 63 if not self.page: 64 return [] 65 links = self.page.eval_on_selector_all( 66 "a[href]", 67 "elements => elements.map(e => ({text: e.textContent, href: e.href}))" 68 ) 69 return links 70 71 def close_browser(self) -> str: 72 """Close the browser and clean up""" 73 if self.browser: 74 self.browser.close() 75 if self.playwright: 76 self.playwright.stop() 77 return "Browser closed" 78 79# Create the browser tool instance 80browser = BrowserTool() 81 82# Create agent with browser tool 83agent = Agent( 84 "browser-agent", 85 tools=[browser], 86 system_prompt="""You are a browser automation assistant. 87 Help users navigate websites, take screenshots, and extract content. 88 Always start the browser before performing actions. 89 Be helpful and explain what you're doing.""" 90) 91 92if __name__ == "__main__": 93 # Example usage 94 print("🌐 Browser Automation Agent") 95 print("=" * 50) 96 97 # Start browser 98 result = agent.input("Start the browser in headless mode") 99 print(f"✅ {result}") 100 101 # Navigate to a website 102 result = agent.input("Navigate to https://example.com") 103 print(f"✅ {result}") 104 105 # Take screenshot 106 result = agent.input("Take a full page screenshot and save as example.png") 107 print(f"✅ {result}") 108 109 # Extract content 110 result = agent.input("Extract all the links from the page") 111 print(f"📋 Links found: {result}") 112 113 # Clean up 114 result = agent.input("Close the browser") 115 print(f"✅ {result}")

Interactive Demo Script

demo.py
1#!/usr/bin/env python3 2"""Interactive demo of the browser agent""" 3 4from agent import agent, browser 5 6print("🌐 Browser Automation Demo") 7print("=" * 50) 8 9# Step 1: Start browser 10print("Step 1: Starting browser...") 11result = agent.input("Start the browser (not headless so we can see it)") 12print(f"✅ {result}\n") 13 14# Step 2: Navigate to documentation 15print("Step 2: Navigating to ConnectOnion docs...") 16result = agent.input( 17 "Navigate to https://docs.connectonion.com and tell me the page title" 18) 19print(f"✅ {result}\n") 20 21# Step 3: Take screenshot 22print("Step 3: Taking a screenshot...") 23result = agent.input("Take a screenshot and save it as docs_homepage.png") 24print(f"✅ {result}\n") 25 26# Step 4: Extract navigation links 27print("Step 4: Extracting navigation links...") 28result = agent.input( 29 "Extract all navigation links and tell me what sections are available" 30) 31print(f"📋 {result}\n") 32 33# Step 5: Navigate to examples 34print("Step 5: Going to examples section...") 35result = agent.input( 36 "Click on the Examples link if available and tell me what you see" 37) 38print(f"✅ {result}\n") 39 40# Step 6: Full page screenshot 41print("Step 6: Taking full page screenshot...") 42result = agent.input( 43 "Take a full page screenshot of the examples and save as examples_full.png" 44) 45print(f"✅ {result}\n") 46 47# Clean up 48print("Cleaning up...") 49result = agent.input("Close the browser") 50print(f"✅ {result}") 51 52print("\n" + "=" * 50) 53print("Demo complete! Check out:") 54print(" - docs_homepage.png") 55print(" - examples_full.png")
Python REPL
Interactive
🌐 Browser Automation Demo
==================================================
Step 1: Starting browser...
✅ Browser started successfully
 
Step 2: Navigating to ConnectOnion docs...
✅ Navigated to https://docs.connectonion.com - Title: "ConnectOnion Documentation"
 
Step 3: Taking a screenshot...
✅ Screenshot saved to docs_homepage.png
 
Step 4: Extracting navigation links...
📋 Found 12 sections: Getting Started, Core Concepts, Advanced Features, Examples, Blog, Roadmap
 
Step 5: Going to examples section...
✅ Clicked Examples link - Now viewing example projects
 
Step 6: Taking full page screenshot...
✅ Full page screenshot saved to examples_full.png
 
Cleaning up...
✅ Browser closed
 
==================================================
Demo complete! Check out:
- docs_homepage.png
- examples_full.png

Common Use Cases

Website Monitoring

main.py
1# Monitor website changes 2agent.input("Navigate to status.example.com") 3agent.input("Take a screenshot and save with timestamp") 4agent.input("Extract the status text and check if all systems operational")
Python REPL
Interactive
Navigated to status.example.com
Screenshot saved: status_20250906_150000.png
Status: All systems operational ✅

Data Extraction

main.py
1# Scrape product information 2agent.input("Navigate to shop.example.com/products") 3agent.input("Extract all product names and prices") 4agent.input("Save the data to products.json")
Python REPL
Interactive
Navigated to shop.example.com/products
Extracted 25 products with prices
Data saved to products.json

Form Automation

main.py
1# Fill and submit forms 2agent.input("Navigate to example.com/contact") 3agent.input("Fill the name field with 'John Doe'") 4agent.input("Fill the email field with 'john@example.com'") 5agent.input("Click the submit button")
Python REPL
Interactive
Navigated to example.com/contact
Filled name field
Filled email field
Form submitted successfully

Installation

Install dependencies
$pip install connectonion
$pip install playwright
$playwright install

Pro Tips

  • Use headless mode for production to save resources
  • Add waits for dynamic content: page.wait_for_selector()
  • Handle errors gracefully with try-except blocks
  • Use specific selectors for reliable element targeting
  • Clean up resources with browser.close()

Ready to Automate the Web?

Start building your own browser automation agents with ConnectOnion