-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaywright_agent.py
More file actions
81 lines (63 loc) · 2.33 KB
/
playwright_agent.py
File metadata and controls
81 lines (63 loc) · 2.33 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
#!/usr/bin/env python3
"""
Playwright AI Agent - browser automation guided by Copilot.
Requires: pip install playwright && python -m playwright install chromium
"""
import asyncio
import sys
from copilot import CopilotClient
async def main():
"""Run the Playwright AI agent."""
if len(sys.argv) < 2:
print("Usage: python playwright_agent.py <url> [task]")
print("\nExamples:")
print(" python playwright_agent.py https://example.com")
print(' python playwright_agent.py https://github.com "Find trending repositories"')
sys.exit(1)
url = sys.argv[1]
task = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else "Describe what you see on the page"
print("🌐 Playwright AI Agent")
print(f"📍 Target: {url}")
print(f"🎯 Task: {task}\n")
try:
from playwright.async_api import async_playwright
except ModuleNotFoundError:
print("❌ This sample requires Playwright.")
print(" Install it with:")
print(" pip install playwright")
print(" python -m playwright install chromium")
sys.exit(1)
# Launch browser
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
try:
# Navigate to the URL
await page.goto(url)
page_title = await page.title()
page_content = await page.content()
print(f"✅ Loaded: {page_title}\n")
# Set up Copilot
client = CopilotClient()
await client.start()
try:
session = await client.create_session({"model": "gpt-5-mini"})
prompt = f"""You are a browser automation agent. Current page: {url}
Page title: {page_title}
Page HTML (excerpt):
```html
{page_content[:3000]}
```
Task: {task}
Analyze the page content and complete the task."""
print("🤖 Agent:\n")
response = await session.send_and_wait({"prompt": prompt})
print(response.data.content)
print("\n✅ Task complete!")
await session.destroy()
finally:
await client.stop()
finally:
await browser.close()
if __name__ == "__main__":
asyncio.run(main())