Docs/Element discovery
#

Element discovery

Use this when a snapshot contains the expected word but the control you expect cannot be found or clicked.

Strategy: weak assumptions to strong evidence

  1. Search the shortest distinctive term across every element.
  2. Rank matches by direct evidence: aria-label, title, short visible text, then data-testid.
  3. Inspect only the matching element's local neighborhood.
  4. Check both directions: the nearest interactive ancestor and any interactive descendants.
  5. Add role, tag, and uniqueness constraints only after observing structure.

Do not search for "button", "input", or another presumed tag first. Components often put labels or tooltips on wrappers and nest an icon-only control inside.

Live DOM

Start with the semantic term:

bash
cofactor session snapshot --search "Delete"

Find direct DOM signals without assuming a tag (keep text matches short so large containers do not dominate):

bash
cofactor session evaluate --js "() => {
  const needle = /delete/i;
  return [...document.querySelectorAll('*')].flatMap((el) => {
    const text = el.children.length === 0 ? el.textContent?.trim() : '';
    const signals = {
      ariaLabel: el.getAttribute('aria-label'),
      title: el.getAttribute('title'),
      text: text && text.length <= 120 ? text : null,
      testId: el.getAttribute('data-testid'),
    };
    return Object.values(signals).some((v) => v && needle.test(v))
      ? [{ tag: el.tagName, signals, html: el.outerHTML.slice(0, 1200) }]
      : [];
  });
}"

Then inspect the match in both directions, because the label may sit outside the clickable control:

bash
cofactor session evaluate --js "() => {
  const match = document.querySelector('[aria-label*=\"Delete\" i]');
  const interactive = 'button, a, input, select, textarea, [role=\"button\"], [tabindex]';
  return {
    match: match?.outerHTML,
    parent: match?.parentElement?.outerHTML.slice(0, 1600),
    interactiveAncestor: match?.closest(interactive)?.outerHTML,
    interactiveDescendants: [...(match?.querySelectorAll(interactive) ?? [])]
      .map((el) => el.outerHTML),
  };
}"

Choose the interaction from what comes back:

text
label on the button:             getByRole('button', { name: /delete/i })
label on a wrapper with a button: locator('[aria-label*="Delete" i]').locator('button')
text inside a button:            getByRole('button').filter({ hasText: /delete/i })

Perform real interactions with session run-code; use evaluate only to inspect.

Negative-evidence gate

Do not report a control as missing unless all of these hold in the expected UI state:

  • The screenshot does not show it.
  • The snapshot has no matching term, icon description, tooltip, or nearby control.
  • The live DOM has no matching label, title, short text, test ID, or state transition.

If the term exists but the expected role does not, report a locator or accessibility-structure mismatch, not missing functionality.