Complete example of browser-use CDP workflow.
()
| 20 | |
| 21 | |
| 22 | async def example_workflow(): |
| 23 | """Complete example of browser-use CDP workflow.""" |
| 24 | |
| 25 | print("🚀 Browser Use CDP - Complete Workflow Example") |
| 26 | print("=" * 55) |
| 27 | |
| 28 | sessions_dir = Path(__file__).parent / "sessions" |
| 29 | storage_state_file = sessions_dir / "auth_state.json" |
| 30 | |
| 31 | # Step 1: Check if we have saved authentication |
| 32 | if not storage_state_file.exists(): |
| 33 | print("📋 Step 1: Setting up authentication session") |
| 34 | print("-" * 40) |
| 35 | |
| 36 | response = input("No saved session found. Would you like to set one up? (y/n): ") |
| 37 | if response.lower() == 'y': |
| 38 | print("Opening browser for manual login...") |
| 39 | try: |
| 40 | subprocess.run([ |
| 41 | "uv", "run", "setup_session.py" |
| 42 | ], check=True) |
| 43 | except subprocess.CalledProcessError: |
| 44 | print("❌ Failed to run setup script") |
| 45 | return |
| 46 | else: |
| 47 | print("❌ Cannot proceed without authentication session") |
| 48 | return |
| 49 | |
| 50 | # Step 2: Create browser session with saved authentication |
| 51 | print("\n📋 Step 2: Creating browser session with saved authentication") |
| 52 | print("-" * 55) |
| 53 | |
| 54 | try: |
| 55 | profile = BrowserProfile( |
| 56 | headless=False, |
| 57 | storage_state=str(storage_state_file), |
| 58 | keep_alive=True, |
| 59 | # Stealth options to avoid detection |
| 60 | channel="chrome", |
| 61 | args=[ |
| 62 | "--disable-blink-features=AutomationControlled", |
| 63 | "--disable-features=VizDisplayCompositor", |
| 64 | ], |
| 65 | extra_http_headers={ |
| 66 | "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" |
| 67 | }, |
| 68 | ) |
| 69 | |
| 70 | session = BrowserSession(browser_profile=profile) |
| 71 | await session.start() |
| 72 | |
| 73 | print("✅ Browser session created successfully") |
| 74 | |
| 75 | # Step 3: Navigate to target site |
| 76 | print("\n📋 Step 3: Navigating to X.com with authenticated session") |
| 77 | print("-" * 50) |
| 78 | |
| 79 | page = await session.get_page() |