microsoft/playwright · error · Error

"${param.target}" does not match any elements.

Error message

"${param.target}" does not match any elements.

What it means

Thrown by Tab.targetLocator when params.target does NOT look like an aria ref (does not match /^(f\d+)?e\d+$/), so it is treated as a CSS/locator string, converted to a selector, and page.$(selector) returns null — meaning no element matches in the DOM.

Source

Thrown at packages/playwright-core/src/tools/backend/tab.ts:504

  async waitForCompletion(callback: () => Promise<void>) {
    await this._initializedPromise;
    await this._raceAgainstModalStates(() => waitForCompletion(this, callback));
  }

  async targetLocator(params: { element?: string, target: string }): Promise<{ locator: playwright.Locator, resolved: string, selector: string }> {
    await this._initializedPromise;
    return (await this.targetLocators([params]))[0];
  }

  async targetLocators(params: { element?: string, target: string }[]): Promise<{ locator: playwright.Locator, resolved: string, selector: string }[]> {
    await this._initializedPromise;
    return Promise.all(params.map(async param => {
      if (!param.target.match(/^(f\d+)?e\d+$/)) {
        const selector = locatorOrSelectorAsSelector('javascript', param.target, this.context.config.testIdAttribute || 'data-testid');
        const handle = await this.page.$(selector);
        if (!handle)
          throw new Error(`"${param.target}" does not match any elements.`);
        handle.dispose().catch(() => {});
        return { locator: this.page.locator(selector), resolved: asLocator('javascript', selector), selector };
      } else {
        try {
          let locator = this.page.locator(`aria-ref=${param.target}`);
          if (param.element)
            locator = locator.describe(param.element);
          const resolved = await locator.normalize();
          return { locator, resolved: resolved.toString(), selector: locatorSelector(resolved) };
        } catch (e) {
          throw new Error(`Ref ${param.target} not found in the current page snapshot. Try capturing new snapshot.`);
        }
      }
    }));
  }

  async waitForTimeout(time: number) {
    if (this._javaScriptBlocked()) {

View on GitHub (pinned to c8fc3bf8d3)

Solutions

  1. Capture a fresh snapshot with browser_snapshot and use the returned element ref (e.g. e12) instead of a hand-written selector.
  2. If using a raw selector, verify it with page.locator(selector).count() > 0 before calling the tool.
  3. Correct typos or update the selector to match the current DOM (including shadow-piercing >> combinators if needed).

Example fix

// before
await client.callTool('browser_click', { element: 'go', target: 'buttton#go' }); // typo -> no match -> throws

// after
await client.callTool('browser_click', { element: 'go', target: 'e12' }); // ref from latest snapshot
Defensive patterns

Strategy: validation

Validate before calling

import { locatorOrSelectorAsSelector } from '...';
async function ensureSelectorMatches(page, target, testIdAttribute) {
  const selector = locatorOrSelectorAsSelector('javascript', target, testIdAttribute || 'data-testid');
  const handle = await page.$(selector);
  return !!handle;
}
if (await ensureSelectorMatches(page, target, 'data-testid')) {
  await client.callTool('browser_click', { element, target });
}

Type guard

function looksLikeRef(target: string): boolean {
  return /^(f\d+)?e\d+$/.test(target);
}

Try / catch

try {
  await client.callTool('browser_click', { element, target });
} catch (e) {
  if (e instanceof Error && e.message.endsWith('does not match any elements.')) {
    // re-snapshot and retry with a fresh ref
    await client.callTool('browser_snapshot', {});
  } else throw e;
}

Prevention

When it happens

Trigger: Passing a raw CSS selector or Playwright locator string (e.g. "button#go", "text=Submit") that does not match any element at the time targetLocator runs.

Common situations: Stale or mistyped selector; the element is in a shadow DOM not reachable by the selector; the page has not finished loading the target; ref string was hand-edited and no longer matches the ref pattern.

Related errors


AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12). Data as JSON: /api/errors/7278d65dd42bc472. Report an issue: GitHub.