Drive a Browser with Tools
Why This Matters
The planner, generator and healer are built from the same small set of browser tools. When an agent does something odd, such as clicking the wrong button, missing an element or claiming a page is empty, the reason is almost always visible in one tool call and its result. Knowing what a snapshot contains, what a ref is and when the page was captured lets you read an agent’s work, and correct it.
What you will learn
- What
navigate_toandcapture_pagereturn: the accessibility tree with refs and selectors - How to act on elements by ref, and why refs change
- Why a snapshot can show a page that is not ready yet, and what to do about it
- How the expectation system and
batch_executekeep the AI’s context small
All output in this lesson is real, from Selenium AI Agent on practiceautomatedtesting.com.
A page as text
Ask your assistant: “Navigate to https://practiceautomatedtesting.com/shopping." It calls navigate_to, and the result contains a snapshot (shortened here):
Page: E-commerce Testing Practice - Shopping Application | Practice Automated Testing
URL: https://practiceautomatedtesting.com/shopping
- main "..." [ref=e68, ...]
- heading "E-commerce Testing Practice" [level=1] [ref=e12, xpath=//h1[normalize-space()='E-commerce Testing Practice']]
- button "shopping_cart0 items$0.00" [ref=e13, css=button[data-testid="cart-button"]]
- textbox "Search for items" [ref=e16, css=input[data-testid="search-input"]]
- combobox "AllElectronicsAccessoriesOffice SuppliesFurniture" [ref=e18, css=select[data-testid="category-filter"]]
- checkbox "In Stock Only" [ref=e23, css=input[data-testid="in-stock-toggle"]]
- heading "Search Results (0 products)" [level=2] [ref=e28, css=h2:has(> span[data-testid="results-count"])]
- generic "Loading products…" [ref=e79, css=div[data-testid="products-loading"]]
Every line has:
- the role (
button,textbox,heading) and the accessible name: what a screen reader would announce, so roughly what a user sees - a ref (
e13): a short handle the AI uses in the next tool call - a selector the server computed for that element: here test ids first (
[data-testid="cart-button"]), XPath on visible text where there is no better option. Lesson 12 explains how it chooses.
This text is what the AI “sees”. It never gets the HTML, and it doesn’t need screenshots to act.
Read it like a tester
Look at the last two lines: “Search Results (0 products)” and “Loading products…”. The snapshot was taken right after the page loaded, before the product list arrived from the shop’s API. An agent that believed this snapshot would conclude that the shop is empty.
The fix is the same as in any Selenium test: wait for the condition you need. Ask the agent to wait for an Add to Cart button (wait_for with element_visible on [data-testid="add-to-cart-2"]):
Condition met: element_visible "[data-testid="add-to-cart-2"]"
...
- button "shopping_cart0 items$0.00" [ref=e2, css=button[data-testid="cart-button"]]
- textbox "Search for items" [ref=e5, css=input[data-testid="search-input"]]
- heading "Search Results (10 products)" [level=2] [ref=e17, ...]
Now there are 10 products. Notice also that the refs changed: the cart button was e13 and is now e2. Refs are numbered per snapshot, so they are only valid until the next one. The selectors ([data-testid="cart-button"]) are what stays stable, and they are what ends up in generated tests.
Act on elements
Tools act by ref: click_element, input_text, select_option, hover_element, key_press. And they check: verify_text_visible, verify_element_visible, verify_value, verify_list_visible.
Here is a search, as one batch of two steps (real result):
batch_execute
1. input_text ref e5, text "keyboard"
2. verify_text_visible "Search Results (1 products)"
[OK] Step 1 (input_text): Typed "keyboard" into e5
[FAIL] Step 2 (verify_text_visible): Text "Search Results (1 products)" not found:
Waiting for element to be located By(xpath, //*[contains(text(), 'Search Results (1 products)')])
Wait timed out after 5136ms
The search worked, yet the check failed. The error shows why: verify_text_visible looks for an element whose own text contains the string. The heading is built from three pieces, Search Results (, a <span> with 1, and products), so no single text node contains the whole sentence. The snapshot showed it: the heading has a child generic "1" with data-testid="results-count".
Checking something that is one text node works:
[OK] Step 1 (verify_text_visible): Text "Mechanical Keyboard" is visible (found 1 visible occurrence(s))
The same lesson applies to the tests the agent generates: an assertion on text split over several elements needs the element’s full text (.text in Selenium), or the specific child ([data-testid="results-count"] with value 1). A good agent learns this from the failure. A tester recognises it immediately.
Keep the context small
Every tool result goes into the AI’s context, and a full snapshot of this page is several thousand characters. Two mechanisms keep it manageable.
The expectation parameter. Every tool accepts expectation, which says what to return:
{
"includeSnapshot": true,
"snapshotOptions": { "selector": "[data-testid=product-grid]", "maxLength": 1200 },
"includeConsole": true,
"consoleOptions": { "levels": ["error"] }
}
With a scoped snapshot, the result after the search is just the product grid:
- generic "Mechanical KeyboardElectronics★★★★★(5)A tactile mechanica..." [ref=e5, css=div[data-testid="product-grid"]]
- generic "..." [ref=e4, css=div[data-testid="product-2"]]
- img "Mechanical Keyboard" [ref=e1, css=img[alt="Mechanical Keyboard"]]
- heading "Mechanical Keyboard" [level=3] [ref=e2, xpath=//h3[normalize-space()='Mechanical Keyboard']]
- button "Add to Cart" [ref=e3, css=button[data-testid="add-to-cart-2"]]
batch_execute runs up to 20 steps in one round trip, with a snapshot only after the last one. Fewer round trips, less context, faster agents.
Tool groups. The server loads all tool groups by default: about 65 tool definitions sent to the model with every request. If you only browse and heal, load less:
"env": { "SELENIUM_MCP_TOOLSETS": "core,healer" }
Exercise
With your assistant and the agent:
- Navigate to the shop and ask for a snapshot scoped to the filters (the
asideelement). Which refs and selectors do the “In Stock Only” and “On Sale Only” checkboxes have? - Tick “On Sale Only” and check how many products remain, without relying on a sentence that is split over several elements.
Show suggested answer
- The checkboxes appear as
checkbox "In Stock Only" [ref=…, css=input[data-testid="in-stock-toggle"]]andcheckbox "On Sale Only" [ref=…, css=input[data-testid="on-sale-toggle"]]. Your refs will differ (they are numbered per snapshot); the selectors are the same for everyone. - Click the “On Sale Only” checkbox by its ref, then read the count from the element with
data-testid="results-count", for example by scoping the next snapshot to it, or withverify_value/execute_javascripton that element. Checking the full heading text withverify_text_visiblefails for the reason shown in this lesson. Compare the count with the number of products that show a SALE badge.
Exam-style questions
1. An agent receives a snapshot in which a list shows “Loading…” and 0 results. What should it do before concluding the list is empty?
- A. Report a bug
- B. Wait for a condition that shows the list has loaded, then take a new snapshot
- C. Reload the browser
- D. Increase the snapshot length
Show answer
2. Why should generated tests use the selector from a snapshot, not the ref?
- A. Refs are longer
- B. Refs are numbered per snapshot and change; selectors identify the element in any session
- C. Selenium cannot click by selector
- D. Refs only work in Chrome