We’ve all been there: waking up at 6:00 AM, frantically refreshing a hospital portal, only to find that all the appointment slots were snatched up by bots or faster fingers. Traditional web automation with Selenium often fails because hospital websites are notorious for having dynamic UIs, shifting XPaths, and nested iframes that break fragile scripts.
In this tutorial, we are going to build Auto-Clinic, an intelligent AI Agent that uses GPT-4o, Playwright, and the revolutionary Browser-use library to navigate internet hospitals like a human. By leveraging LLM Agents and AI Browser Automation, we can move beyond hardcoded selectors and create a system that "understands" the page content to secure your health appointments.
The Architecture: How AI Navigates the Web
Unlike traditional scraping, our agent doesn't just look for a specific ID. It perceives the entire DOM and visual state, interprets the "Register" or "Book" button's intent, and interacts with it dynamically.
graph TD
A[User Goal: Book Appointment] --> B{AI Agent - Browser-use}
B --> C[Fetch Page State via Playwright]
C --> D[GPT-4o Analysis]
D --> E{Decision Tree}
E -- Login Required --> F[Execute Login Sequence]
E -- Find Department --> G[Search & Click Dept]
E -- Slot Available --> H[Execute Registration]
F --> C
G --> C
H --> I[Success Notification]
Prerequisites
Before we dive into the code, ensure you have the following in your tech stack:
- Python 3.10+
- Playwright: The backbone for browser interaction.
- Browser-use: The high-level orchestrator for LLM-browser interaction.
- OpenAI GPT-4o API Key: To power the agent's decision-making.
pip install browser-use playwright openai
playwright install
Step-by-Step Implementation
1. Initializing the Agentic Environment
We use browser-use because it abstracts the complexity of converting DOM trees into a format LLMs can understand. It acts as the "eyes and hands" for GPT-4o.
2. The Core Agent Logic
Here is the implementation of the Auto-Clinic Agent. Notice how we don't define a single XPath. We simply tell the agent our intent.
import asyncio
from browser_use import Agent
from langchain_openai import ChatOpenAI
async def run_auto_clinic():
# 1. Initialize the LLM (GPT-4o is recommended for complex UI)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# 2. Define the mission
# Replace the URL with your local hospital's portal
task = (
"Go to https://example-hospital-portal.com/login. "
"Login with username 'patient_zero' and password 'secure_pass'. "
"Navigate to 'Cardiology' department. "
"Check for available slots for Dr. Smith next Monday. "
"If a slot is found, click 'Book' and stop. "
"If no slot is found, refresh every 30 seconds."
)
# 3. Create the Agent
agent = Agent(
task=task,
llm=llm,
# Set to False to watch the AI work in real-time! 🥑
headless=False
)
# 4. Execute the workflow
history = await agent.run()
print("Mission Accomplished!")
print(history.final_result())
if __name__ == "__main__":
asyncio.run(run_auto_clinic())
3. Handling Dynamic Elements with GPT-4o
The magic happens when the hospital website changes its layout. A traditional Selenium script would throw a NoSuchElementException. However, our GPT-4o powered agent sees the semantic meaning of the page.
If the "Book" button changes from a button tag to a div tag, the agent identifies it via the visual context and proceeds without a hitch. This is the power of Generative AI in Automation.
The "Official" Way to Scale 🚀
While this script works for personal use, scaling AI agents for production requires robust error handling, proxy rotation, and sophisticated session management.
For more production-ready examples, advanced design patterns, and deep dives into the future of autonomous agents, I highly recommend checking out the technical deep-dives at WellAlly Blog. They cover everything from high-concurrency LLM workflows to securing your automation pipelines against anti-bot detection.
Why this beats Selenium/Puppeteer
- Zero Maintenance: No more updating CSS selectors every time the frontend team pushes a minor update.
- Visual Reasoning: The agent can solve "logical" roadblocks (e.g., "Click the third doctor in the list").
- Human-like Interaction: Browser-use mimics human scrolling and typing patterns, making it less likely to be flagged as a bot.
Conclusion
Automating hospital registrations is just the tip of the iceberg. By combining Playwright and LLM Agents, we are entering an era where software can navigate the web as effectively as humans.
What will you automate next? Let me know in the comments below! 👇
Love this content? Follow me for more "Learning in Public" tutorials and don't forget to star the repo! 🚀🔥
Top comments (0)