Created
September 12, 2025 06:31
-
-
Save ruseel/e249c8546662eff753fab0ec615e5a9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Test script to browser-use. | |
| This script attempts to: | |
| 1. Search for flights to Singapore on Google Flights | |
| 2. Set departure date to September 17th and return date to September 24th | |
| 3. Extract top 4 flight options in JSON format with keys: | |
| airline, departure_time, arrival_time, duration, total_amount, route | |
| """ | |
| import asyncio | |
| import os | |
| import json | |
| from datetime import datetime | |
| from browser_use import Agent, ChatOpenAI, BrowserProfile | |
| import logging | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| async def test_flight_search(): | |
| # Enable detailed logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
| ) | |
| # Check if we have API keys | |
| openai_key = os.getenv('OPENAI_API_KEY') | |
| assert openai_key | |
| task = """ | |
| Search for flights from 'Seoul' to "Singapore" on Google Flights (google.com/travel/flights) for 1 adult. | |
| Set departure date to September 17th and return date to September 24th. | |
| Extract top 4 flight options and return them in JSON format with these exact keys: | |
| airline, departure_time, arrival_time, duration, total_amount, route | |
| Return only the JSON array, nothing else. | |
| """ | |
| try: | |
| print("π« Starting flight search automation with browser-use...") | |
| # Initialize ChatOpenAI with Claude Sonnet 4 via OpenRouter | |
| llm = ChatOpenAI( | |
| model="openai/gpt-4.1-mini", | |
| api_key=openai_key, | |
| base_url="https://openrouter.ai/api/v1" | |
| ) | |
| # Create browser profile | |
| browser_profile = BrowserProfile( | |
| headless=True, | |
| highlight_elements=True, # Enable boxes and numbers around clickable elements | |
| disable_security=True, | |
| args=[ | |
| "--no-sandbox", | |
| "--disable-dev-shm-usage", | |
| "--enable-logging", | |
| "--log-level=0", | |
| "--v=1" | |
| ] | |
| ) | |
| print("π§ Creating agent...") | |
| # Create browser-use agent with GIF generation | |
| agent = Agent( | |
| task=task, | |
| llm=llm, | |
| browser_profile=browser_profile, | |
| generate_gif="flight_search.gif" # Generate GIF with visual indicators | |
| ) | |
| print("π Running agent...") | |
| result = await agent.run() | |
| print("β Agent completed!") | |
| print(f"π Result type: {type(result)}") | |
| print(f"π Result: {result}") | |
| # Try to extract any JSON from the result | |
| if hasattr(result, 'all_results') and result.all_results: | |
| print(f"π Found {len(result.all_results)} results") | |
| for i, res in enumerate(result.all_results): | |
| print(f" Result {i+1}: {res}") | |
| if hasattr(res, 'extracted_content') and res.extracted_content: | |
| print(f" π Extracted content: {res.extracted_content}") | |
| # Try to parse final result as JSON if possible | |
| result_str = str(result) | |
| if '{' in result_str and '}' in result_str: | |
| try: | |
| # Extract JSON-like content | |
| start = result_str.find('{') | |
| end = result_str.rfind('}') + 1 | |
| json_candidate = result_str[start:end] | |
| json_result = json.loads(json_candidate) | |
| print(f"π Extracted JSON: {json.dumps(json_result, indent=2)}") | |
| except json.JSONDecodeError: | |
| print("β οΈ Could not parse JSON from result") | |
| return result | |
| except Exception as e: | |
| print(f"β Error during flight search: {str(e)}") | |
| return None | |
| if __name__ == "__main__": | |
| asyncio.run(test_flight_search()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment