#33 AI agents and MCP 6.4 min
Browser Agents: Fill a Web Form with Playwright
A browser script tied to one page's ids can break the day the page changes. On my two test pages, a loop that looks first did not.
Ask decide what to do first
Copy the code folder first. This needs no browser. In a new file try.py in the code folder, write from agent import decide, then build a list seen with three tuples: ('button', 'Place order', 'B'), ('checkbox', 'I accept the returns policy', 'C'), ('textbox', 'Full name', 'N'). Predict, then print decide(seen, set()). I ran it and the result was ('fill', 'Full name', 'name', 'N'): the button is first in the list, but the loop does not choose it.
A hint
decide looks for a banner button, then fields, then the checkbox, and only last the order button.
Tick the box before the order
In try.py call decide(seen, {'name','email','part','qty'}) with the same list and predict the answer, then call it again with 'agree' added to the set. I ran these: the first gives a check action on the returns policy label, and the second gives a click on Place order. Say in one sentence why the order button is never chosen while the box is unticked.
A hint
In decide, the checkbox loop comes before the order button loop.
A label the rules do not know
In try.py call decide with a list holding only ('spinbutton', 'Amount', 'Q') and the set {'name','email','part'}. Predict, then run it. I got None, which would end the loop. Now change the qty pattern in FIELD in agent.py to "(quantity|many|amount)" and run again. Predict the result first. When you are done, change the pattern back to "(quantity|many)".
A hint
The patterns in FIELD are matched against the label, ignoring capital letters.
Cut the step limit
In agent.py change the default limit in def agent from 12 to 5. Predict whether the order is placed on /v1 and on /v2, using the action counts from the video (6 and 7). This needs Playwright and Chromium as in the video, so treat it as a prediction unless you have them installed.
A hint
The loop stops after the limit, even if it has not clicked Place order yet.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Two versions of a made-up form that I wrote, served on this computer. The step that decides is plain code with rules that match my labels; no language model is used, so prompt injection, logins, captchas and real sites are not tested.
Up next in AI agents and MCP
More in AI agents and MCP
- #01 Local AI Agent in Python: No API Key (Ollama)
- #12 Multi-Agent Patterns in Plain Python: Pipeline, Router, Loop
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python
- #23 Agent Memory: What to Keep and What to Forget
- #24 Approval Gates for Risky Agent Actions
- #25 Tool Errors: Retries, Fallbacks and Made-Up Answers
Related videos
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python AI agents and MCP
- #232 Give Every AI Agent Its Own Identity Enterprise architecture and AI agents
- #236 Agent Orchestration Patterns: Supervisor, Pipeline, Swarm Enterprise architecture and AI agents
- #237 Where Should AI Agent State and Memory Live? A Crash Test Enterprise architecture and AI agents
