-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathact_example.py
More file actions
71 lines (54 loc) · 2.23 KB
/
act_example.py
File metadata and controls
71 lines (54 loc) · 2.23 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
"""
Example demonstrating calling act() with a string instruction.
This example shows how to use the act() method with a natural language
string instruction instead of an Action object from observe().
The act() method accepts either:
1. A string instruction (demonstrated here): input="click the button"
2. An Action object from observe(): input=action_object
Required environment variables:
- BROWSERBASE_API_KEY: Your Browserbase API key
- BROWSERBASE_PROJECT_ID: Your Browserbase project ID
- MODEL_API_KEY: Your OpenAI API key
"""
import os
from stagehand import AsyncStagehand
async def main() -> None:
# Create client using environment variables
# BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY
async with AsyncStagehand(
browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"),
browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"),
model_api_key=os.environ.get("MODEL_API_KEY"),
) as client:
# Start a new browser session
session = await client.sessions.create(
model_name="openai/gpt-5-nano",
)
print(f"Session started: {session.id}")
try:
# Navigate to example.com
await session.navigate(
url="https://www.example.com",
)
print("Navigated to example.com")
# Call act() with a string instruction directly
# This is the key test - passing a string instead of an Action object
print("\nAttempting to call act() with string input...")
act_response = await session.act(
input="click the 'More information' link", # String instruction
)
print(f"Act completed successfully!")
print(f"Result: {act_response.data.result.message}")
print(f"Success: {act_response.data.result.success}")
except Exception as e:
print(f"Error: {e}")
print(f"Error type: {type(e).__name__}")
import traceback
traceback.print_exc()
finally:
# End the session to clean up resources
await session.end()
print("\nSession ended")
if __name__ == "__main__":
import asyncio
asyncio.run(main())