File size: 11,265 Bytes
5374a2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
#!/usr/bin/env python3
"""
Example demonstrating how to use browser automation toolkits from EvoAgentX.
This script provides comprehensive examples for:
- BrowserToolkit (Selenium-based): Fine-grained control over browser elements
- BrowserUseToolkit (AI-driven): Natural language browser automation
"""
import os
import sys
# import time
from pathlib import Path
from dotenv import load_dotenv
load_dotenv(override=True)
# Add the parent directory to sys.path to import from evoagentx
sys.path.append(str(Path(__file__).parent.parent))
from evoagentx.tools import (
BrowserToolkit,
BrowserUseToolkit
)
def run_browser_tool_example():
"""
Run an example using the BrowserToolkit with auto-initialization and auto-cleanup.
Uses a comprehensive HTML test page to demonstrate browser automation features.
"""
print("\n===== BROWSER TOOL EXAMPLE =====\n")
try:
# Initialize the browser toolkit (browser auto-initializes when first used)
browser_toolkit = BrowserToolkit(headless=False, timeout=10)
# Get individual tools from the toolkit
nav_tool = browser_toolkit.get_tool("navigate_to_url")
input_tool = browser_toolkit.get_tool("input_text")
click_tool = browser_toolkit.get_tool("browser_click")
snapshot_tool = browser_toolkit.get_tool("browser_snapshot")
# Use the static test HTML file
test_file_path = os.path.join(os.getcwd(), "examples", "tools", "browser_test_page.html")
print("Step 1: Navigating to test page (browser auto-initializes)...")
nav_result = nav_tool(url=f"file://{test_file_path}")
print("Navigation Result:")
print("-" * 30)
print(f"Status: {nav_result.get('status')}")
print(f"URL: {nav_result.get('current_url')}")
print(f"Title: {nav_result.get('title')}")
print("-" * 30)
if nav_result.get("status") in ["success", "partial_success"]:
print("\nStep 2: Taking initial snapshot to identify elements...")
snapshot_result = snapshot_tool()
if snapshot_result.get("status") == "success":
print("β Initial snapshot successful")
# Find interactive elements
elements = snapshot_result.get("interactive_elements", [])
print(f"Found {len(elements)} interactive elements")
# Identify specific elements
name_input_ref = None
email_input_ref = None
message_input_ref = None
submit_btn_ref = None
clear_btn_ref = None
test_btn_ref = None
for elem in elements:
desc = elem.get("description", "").lower()
purpose = elem.get("purpose", "").lower()
if "name" in desc and elem.get("editable"):
name_input_ref = elem["id"]
elif "email" in desc and elem.get("editable"):
email_input_ref = elem["id"]
elif "message" in desc and elem.get("editable"):
message_input_ref = elem["id"]
elif "submit" in purpose and elem.get("interactable"):
submit_btn_ref = elem["id"]
elif "clear" in purpose and elem.get("interactable"):
clear_btn_ref = elem["id"]
elif "test" in purpose and elem.get("interactable"):
test_btn_ref = elem["id"]
print(f"Identified elements:")
print(f" - Name input: {name_input_ref}")
print(f" - Email input: {email_input_ref}")
print(f" - Message input: {message_input_ref}")
print(f" - Submit button: {submit_btn_ref}")
print(f" - Clear button: {clear_btn_ref}")
print(f" - Test button: {test_btn_ref}")
# Test input functionality
if name_input_ref and email_input_ref and message_input_ref:
print("\nStep 3: Testing input functionality...")
# Fill name field
print(" - Typing 'John Doe' in name field...")
name_result = input_tool(
element="Name input",
ref=name_input_ref,
text="John Doe",
submit=False
)
print(f" Result: {name_result.get('status')}")
# Fill email field
print(" - Typing 'john.doe@example.com' in email field...")
email_result = input_tool(
element="Email input",
ref=email_input_ref,
text="john.doe@example.com",
submit=False
)
print(f" Result: {email_result.get('status')}")
# Fill message field
print(" - Typing 'This is a test message for browser automation.' in message field...")
message_result = input_tool(
element="Message input",
ref=message_input_ref,
text="This is a test message for browser automation.",
submit=False
)
print(f" Result: {message_result.get('status')}")
# Test form submission
if submit_btn_ref:
print("\nStep 4: Testing form submission...")
submit_result = click_tool(
element="Submit button",
ref=submit_btn_ref
)
print(f"Submit result: {submit_result.get('status')}")
# Take snapshot to see the result
print("\nStep 5: Taking snapshot to verify form submission...")
result_snapshot = snapshot_tool()
if result_snapshot.get("status") == "success":
content = result_snapshot.get("page_content", "")
if "Name: John Doe, Email: john.doe@example.com" in content:
print("β Form submission successful - data correctly displayed!")
else:
print("β Form submission may have failed")
# Test test button click
if test_btn_ref:
print("\nStep 6: Testing test button click...")
test_result = click_tool(
element="Test button",
ref=test_btn_ref
)
print(f"Test button result: {test_result.get('status')}")
# Take snapshot to see the click result
click_snapshot = snapshot_tool()
if click_snapshot.get("status") == "success":
content = click_snapshot.get("page_content", "")
if "Test button clicked at:" in content:
print("β Test button click successful!")
else:
print("β Test button click may have failed")
# Test clear functionality
if clear_btn_ref:
print("\nStep 7: Testing clear functionality...")
clear_result = click_tool(
element="Clear button",
ref=clear_btn_ref
)
print(f"Clear result: {clear_result.get('status')}")
# Take final snapshot
final_snapshot = snapshot_tool()
if final_snapshot.get("status") == "success":
print("β Clear functionality tested")
print("\nβ Browser automation test completed successfully!")
print("β Browser auto-initialization working")
print("β Navigation working")
print("β Input functionality working")
print("β Click functionality working")
print("β Form submission working")
print("β Snapshot functionality working")
else:
print("β Initial snapshot failed")
else:
print("\nβ Navigation failed")
print("\nBrowser will automatically close when the toolkit goes out of scope...")
print("(No manual cleanup required)")
except Exception as e:
print(f"Error running browser tool example: {str(e)}")
print("Browser will still automatically cleanup on exit")
def run_browser_use_tool_example():
"""Simple example using BrowserUseToolkit for browser automation."""
print("\n===== BROWSER USE TOOL EXAMPLE =====\n")
# Check for OpenAI API key
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
print("β OPENAI_API_KEY not found in environment variables")
print("Please set your OpenAI API key: export OPENAI_API_KEY='your-api-key-here'")
return
try:
# Initialize the BrowserUse toolkit
print("Initializing BrowserUseToolkit...")
toolkit = BrowserUseToolkit(model="gpt-4o-mini", headless=False)
browser_tool = toolkit.get_tool("browser_use")
print("β BrowserUseToolkit initialized")
print(f"β Using OpenAI API key: {openai_api_key[:8]}...")
# Execute a simple browser task
print("Executing browser task: 'Go to Google and search for OpenAI GPT-4'...")
result = browser_tool(task="Go to Google and search for 'OpenAI GPT-4'")
if result.get('success'):
print("β Browser task completed successfully")
print(f"Result: {result.get('result', 'No result details')}")
else:
print(f"β Browser task failed: {result.get('error', 'Unknown error')}")
print("\nβ BrowserUseToolkit test completed")
except Exception as e:
print(f"Error: {str(e)}")
print("Note: Make sure you have the required dependencies installed and API keys set up.")
def main():
"""Main function to run all browser tool examples"""
print("===== BROWSER TOOL EXAMPLES =====")
# Run Selenium-based browser example
run_browser_tool_example()
# Run AI-driven browser example
run_browser_use_tool_example()
if __name__ == "__main__":
main() |