microsoft/playwright · error · Error

Tab ${index} not found

Error message

Tab ${index} not found

What it means

selectTab(index) indexes into _tabs; if there is no tab at that index it throws 'Tab <index> not found'. Used by the MCP select_tab tool. Index is zero-based and must correspond to an existing open tab in the context.

Source

Thrown at packages/playwright-core/src/tools/backend/context.ts:178

  }

  currentTabOrDie(): Tab {
    if (!this._currentTab)
      throw new Error('No open pages available.');
    return this._currentTab;
  }

  async newTab(): Promise<Tab> {
    const browserContext = await this.ensureBrowserContext();
    const page = await browserContext.newPage();
    this._currentTab = this._tabs.find(t => t.page === page)!;
    return this._currentTab;
  }

  async selectTab(index: number) {
    const tab = this._tabs[index];
    if (!tab)
      throw new Error(`Tab ${index} not found`);
    await tab.page.bringToFront();
    this._currentTab = tab;
    return tab;
  }

  async ensureTab(): Promise<Tab> {
    await this.ensureBrowserContext();
    const crashed = this._currentTab?.crashed;
    if (crashed) {
      await this._currentTab!.page.close().catch(() => {});
      this._currentTab = undefined;
    }
    if (!this._currentTab)
      await this.newTab();
    if (crashed)
      this._currentTab!.logErrorMessage('Page crashed and was reset to about:blank.');
    await this._currentTab!.waitForInitialized();
    return this._currentTab!;

View on GitHub (pinned to c8fc3bf8d3)

Solutions

  1. List tabs first, then select within bounds; pass 0-based index strictly less than the tab count.
  2. Validate index is a non-negative integer < tab count before calling selectTab.
  3. Refresh the tab list right before selecting to avoid stale indices.
Defensive patterns

Strategy: validation

Validate before calling

function inBounds(index, tabs) { return Number.isInteger(index) && index >= 0 && index < tabs.length; }
if (inBounds(index, await listTabs())) await selectTab(index);

Type guard

function isValidTabIndex(i: number, count: number): boolean { return Number.isInteger(i) && i >= 0 && i < count; }

Try / catch

try { await context.selectTab(index); }
catch (e) { if (/not found/.test(e.message)) { /* refresh list, retry with valid index */ } else throw e; }

Prevention

When it happens

Trigger: Calling select_tab with an index >= number of open tabs, a negative index (yields undefined), or an index computed from a stale snapshot of the tab list (race with open/close).

Common situations: LLM/tool passing an index from an earlier list_tabs that has since changed. Off-by-one (1-based vs 0-based) confusion. Indexing after a tab was closed.

Related errors


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