-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
180 lines (148 loc) · 6.76 KB
/
main.py
File metadata and controls
180 lines (148 loc) · 6.76 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Stagehand + Browserbase: Automated Nurse License Verification - See README.md for full documentation
import json
import os
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright
from pydantic import BaseModel, Field
from stagehand import Stagehand
# Load environment variables
load_dotenv()
class LicenseRecord(BaseModel):
"""Single license verification record"""
name: str = Field(..., description="the name of the license holder")
license_number: str = Field(..., description="the license number")
status: str = Field(..., description="the status of the license")
more_info_url: str = Field(..., description="URL for more information")
class LicenseResults(BaseModel):
"""Collection of license verification results"""
list_of_licenses: list[LicenseRecord] = Field(
..., description="array of license verification results"
)
# License records to verify - add more records as needed
LICENSE_RECORDS = [
{
"Site": "https://pod-search.kalmservices.net/",
"FirstName": "Ronald",
"LastName": "Agee",
"LicenseNumber": "346",
},
]
def main():
"""
Automated nurse license verification using AI-powered browser automation.
Processes multiple license records and extracts verification results.
"""
print("Starting Nurse License Verification Automation...")
# Initialize Stagehand with Browserbase for cloud-based browser automation
client = Stagehand(
browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"),
)
# Start a new session
start_response = client.sessions.start(
model_name="openai/gpt-4.1",
)
session_id = start_response.data.session_id
try:
print("Initializing browser session...")
print("Stagehand session started successfully")
print(f"Watch live: https://browserbase.com/sessions/{session_id}")
# Connect to the browser via CDP
with sync_playwright() as playwright:
browser = playwright.chromium.connect_over_cdp(
f"wss://connect.browserbase.com?apiKey={os.environ['BROWSERBASE_API_KEY']}&sessionId={session_id}"
)
context = browser.contexts[0]
page = context.pages[0] if context.pages else context.new_page()
# Process each license record sequentially
for license_record in LICENSE_RECORDS:
print(
f"Verifying license for: {license_record['FirstName']} {license_record['LastName']}"
)
# Navigate to license verification site
print(f"Navigating to: {license_record['Site']}")
page.goto(license_record["Site"])
page.wait_for_load_state("domcontentloaded")
# Brief timeout to ensure form fields are interactive
page.wait_for_timeout(1000)
# Fill in form fields with license information
print("Filling in license information...")
client.sessions.act(
id=session_id,
input=f'Type "{license_record["FirstName"]}" into the first name field',
)
client.sessions.act(
id=session_id,
input=f'Type "{license_record["LastName"]}" into the last name field',
)
client.sessions.act(
id=session_id,
input=f'Type "{license_record["LicenseNumber"]}" into the license number field',
)
# Submit search
print("Clicking search button...")
client.sessions.act(
id=session_id,
input="Click the search button",
)
# Wait for search results to load
page.wait_for_load_state("domcontentloaded")
page.wait_for_timeout(1000)
# Extract license verification results using inline schema (avoids $ref issues)
print("Extracting license verification results...")
license_schema = {
"type": "object",
"properties": {
"list_of_licenses": {
"type": "array",
"description": "array of license verification results",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "the name of the license holder",
},
"license_number": {
"type": "string",
"description": "the license number",
},
"status": {
"type": "string",
"description": "the status of the license",
},
"more_info_url": {
"type": "string",
"description": "URL for more information",
},
},
"required": ["name", "license_number", "status", "more_info_url"],
},
}
},
"required": ["list_of_licenses"],
}
extract_response = client.sessions.extract(
id=session_id,
instruction="Extract ALL the license verification results from the page, including name, license number and status",
schema=license_schema,
)
print("License verification results extracted:")
print(json.dumps(extract_response.data.result, indent=2))
browser.close()
client.sessions.end(id=session_id)
print("Session closed successfully")
except Exception as error:
print(f"Error during license verification: {error}")
# Provide helpful troubleshooting information
print("\nCommon issues:")
print("1. Check .env file has BROWSERBASE_API_KEY")
print("2. Ensure internet access and license verification site is accessible")
print("3. Verify Browserbase account has sufficient credits")
client.sessions.end(id=session_id)
raise
if __name__ == "__main__":
try:
main()
except Exception as err:
print(f"Application error: {err}")
exit(1)