Skip to content
agentic-browser

Element Discovery

LLMs need to know what's on a page before they can interact with it. The element discovery system scans the DOM and returns a structured list of all interactive elements with selectors you can use directly.

Why?

Without element discovery, an LLM has to:

  • Guess selectors by hand (fragile, often wrong)
  • Parse raw HTML (noisy, token-expensive)
  • Rely on memory from previous sessions (stale if the site changed)

With getInteractiveElements(), the LLM gets a clean list of what's actually there — buttons, links, inputs, and their exact selectors.

Usage

CLI

agentic-browser agent elements
agentic-browser agent elements --roles button,link,input --limit 20
agentic-browser agent elements --selector "#main-content" --visible-only

MCP

Call browser_get_elements with optional filters. MCP responses are compact — visible, actions, and tagName are omitted to reduce token usage, and enabled is omitted when it is true. The response also includes a summary block with countsByRole and primaryActions so an LLM can find the most useful controls quickly. Actions are derivable from role (see Roles and Actions table below).

Programmatic

const result = await core.getInteractiveElements({
  sessionId: session.sessionId,
  roles: ["button", "link"], // optional
  visibleOnly: true, // default: true
  limit: 50, // default: 50
  selector: "#content", // optional: scope to subtree
});

Output Structure

Each element includes a primary selector and optional fallbackSelectors — alternate unique selectors that can be passed to browser_interact for automatic retry if the primary selector breaks.

Selectors may be plain CSS selectors or composed locators. When discovery crosses an open shadow root or a same-origin iframe, it joins the boundary path with >>>, for example iframe[name="checkout"] >>> button[aria-label="Pay"].

Programmatic API returns the full element shape:

{
  "elements": [
    {
      "selector": "#login-btn",
      "fallbackSelectors": ["button[aria-label=\"Sign In\"]", "[data-testid=\"login\"]"],
      "role": "button",
      "tagName": "button",
      "text": "Sign In",
      "actions": ["click"],
      "visible": true,
      "enabled": true
    },
    {
      "selector": "input[name=\"email\"]",
      "role": "input",
      "tagName": "input",
      "text": "",
      "actions": ["click", "type", "press"],
      "visible": true,
      "enabled": true,
      "inputType": "email",
      "placeholder": "Enter your email",
      "currentValue": ""
    },
    {
      "selector": "a[href=\"/docs\"]",
      "role": "link",
      "tagName": "a",
      "text": "Documentation",
      "actions": ["click"],
      "visible": true,
      "enabled": true,
      "href": "/docs"
    }
  ],
  "totalFound": 87,
  "truncated": true
}

MCP responses omit visible, actions, and tagName to reduce token usage:

{
  "elements": [
    {
      "selector": "#login-btn",
      "fallbackSelectors": ["button[aria-label=\"Sign In\"]", "[data-testid=\"login\"]"],
      "role": "button",
      "text": "Sign In",
      "enabled": true
    },
    {
      "selector": "input[name=\"email\"]",
      "role": "input",
      "text": "",
      "enabled": true,
      "inputType": "email",
      "placeholder": "Enter your email",
      "currentValue": ""
    }
  ],
  "totalFound": 87,
  "truncated": true
}

What Gets Detected

The scanner finds elements matching these selectors:

  • a[href] — links
  • button — buttons
  • input:not([type="hidden"]) — inputs (excluding hidden)
  • select — dropdowns
  • textarea — text areas
  • [role="button"], [role="link"], [role="checkbox"], [role="radio"], [role="menuitem"], [role="tab"], [role="switch"] — ARIA roles
  • [onclick], [tabindex] — event handlers
  • [contenteditable="true"] — editable regions

Duplicate elements (e.g. <button role="button">) are deduplicated automatically.

Selector Generation

Each element gets a unique selector, generated with this priority:

  1. #id — if the element has an id attribute
  2. tag[name="..."] — if name is unique
  3. [data-testid="..."] or [data-test-id="..."] — testing attributes
  4. tag[aria-label="..."] — if unique
  5. [data-cy="..."] — Cypress testing convention
  6. [data-test="..."] — generic testing convention
  7. [role="..."][aria-label="..."] — role + aria-label combo
  8. Composite path — anchored at nearest ancestor with id (e.g. #container > div > button) instead of walking to the root

The generated selectors are designed to be stable and usable directly with the interact command.

Nested Roots

If an element is inside an open shadow root or a same-origin iframe, the scanner prefixes the local selector with the boundary selector using >>>:

  • custom-checkout >>> button[aria-label="Pay"]
  • iframe[name="auth"] >>> input[name="email"]

browser_interact and the CLI agent commands understand this format directly.

Fallback Selectors

In addition to the primary selector, each element may include up to 3 fallbackSelectors — alternate unique selectors derived from other attributes. When you pass fallbackSelectors to browser_interact, the system automatically tries each one if the primary selector fails. This provides resilience when sites update their markup.

Roles and Actions

RoleTypical TagsDefault Actions
linka[href]click
buttonbutton, [role="button"]click
inputinputclick, type, press
selectselectclick, select
textareatextareaclick, type, press
checkboxinput[type="checkbox"], [role="checkbox"]toggle
radioinput[type="radio"], [role="radio"]toggle
contenteditable[contenteditable]click, type
custom[onclick], [tabindex]click

Visibility Check

Elements are checked for visibility using:

  • getComputedStyle() — display: none, visibility: hidden, opacity: 0
  • getBoundingClientRect() — zero width/height

Set visibleOnly: false to include hidden elements (useful for debugging).

Using Discovered Selectors

The typical workflow:

# 1. See what's on the page
agentic-browser agent elements --roles button,link
 
# 2. Use a selector from the result (with fallbacks for resilience)
agentic-browser agent click "#login-btn" --fallback 'button[aria-label="Login"]'
 
# 3. After navigation, discover again
agentic-browser agent elements

The selectors returned by element discovery are guaranteed to match exactly one element on the page at the time of discovery.