-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_selenium_usage.py
More file actions
105 lines (85 loc) · 3.82 KB
/
example_selenium_usage.py
File metadata and controls
105 lines (85 loc) · 3.82 KB
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
#!/usr/bin/env python3
"""
Example usage of AutomationService with undetected_chromedriver backend
This demonstrates how to switch between playwright and selenium backends
and perform account registration using the 360.cn site.
"""
import asyncio
import sys
from pathlib import Path
# Add the project root to sys.path for imports
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from src.services.automation_service import AutomationService
from src.models.account import Account
def log_callback(message: str):
"""Callback function to handle log messages"""
print(f"[LOG] {message}")
def account_start_callback(account: Account):
"""Callback function called when account processing starts"""
print(f"[START] Processing account: {account.username}")
def account_complete_callback(account: Account):
"""Callback function called when account processing completes"""
status = account.status.value
print(f"[COMPLETE] Account {account.username}: {status}")
if hasattr(account, 'failure_reason') and account.failure_reason:
print(f"[ERROR] Reason: {account.failure_reason}")
async def main():
"""Main example function"""
print("=== AutomationService Backend Comparison Example ===\n")
# Create automation service with default backend (playwright)
automation_service = AutomationService()
# Set up callbacks
automation_service.set_callbacks(
on_log_message=log_callback,
on_account_start=account_start_callback,
on_account_complete=account_complete_callback
)
print(f"Available backends: {automation_service.get_available_backends()}")
print(f"Current backend: {automation_service.get_backend()}")
print(f"Selenium available: {automation_service.is_selenium_available()}\n")
# Generate test accounts
print("Generating test accounts...")
test_accounts = automation_service.generate_test_accounts(2)
if not test_accounts:
print("Failed to generate test accounts. Exiting.")
return
print(f"Generated {len(test_accounts)} test accounts:")
for account in test_accounts:
print(f" - {account.username} / {account.password}")
print()
# Test with Playwright backend (default)
print("=== Testing with Playwright Backend ===")
try:
result = await automation_service.register_single_account(test_accounts[0])
print(f"Playwright registration result: {result}")
print(f"Account status: {test_accounts[0].status.value}\n")
except Exception as e:
print(f"Playwright registration failed: {e}\n")
# Switch to Selenium backend if available
if automation_service.is_selenium_available():
print("=== Switching to Selenium Backend ===")
try:
automation_service.set_backend("selenium")
print(f"Switched to backend: {automation_service.get_backend()}")
# Test with Selenium backend
print("=== Testing with Selenium/undetected_chromedriver Backend ===")
result = await automation_service.register_single_account(test_accounts[1])
print(f"Selenium registration result: {result}")
print(f"Account status: {test_accounts[1].status.value}")
except Exception as e:
print(f"Selenium backend error: {e}")
else:
print("=== Selenium Backend Not Available ===")
print("To enable selenium backend, install undetected_chromedriver:")
print("pip install undetected-chromedriver")
print("\n=== Example Complete ===")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nExample interrupted by user.")
except Exception as e:
print(f"Example failed with error: {e}")
import traceback
traceback.print_exc()