learning-it by example

#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.

1. What four things does a browser agent do again and again?
2. In the Playwright demo, on the second version of the made-up bike shop order form (no ids, different labels), what happened to the fixed script and to the looping agent?
3. In the first version of the Playwright form-filling loop, why did it place orders without ticking the returns box?
4. What does the Playwright browser-agent demo say its result does not show?
5. Why does the Playwright browser-agent code check the kind of action with a plain if, and not an assert?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. 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.

  2. 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.

  3. 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.

  4. 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

All AI agents and MCP videos

Related videos