microsoft/playwright · error · Error

Locators must belong to the same frame.

Error message

Locators must belong to the same frame.

What it means

Error "Locators must belong to the same frame." thrown in microsoft/playwright.

Source

Thrown at packages/playwright-core/src/client/locator.ts:180

    // VS Code extension uses this one, keep it for now.
    return await this._frame._highlight(this._selector);
  }

  async highlight(options: { style?: string | Record<string, string | number> } = {}) {
    const style = typeof options.style === 'object' ? cssObjectToString(options.style) : options.style;
    await this._frame._highlight(this._selector, style);
    return new DisposableStub(() => this.hideHighlight());
  }

  async hideHighlight() {
    await this._frame._hideHighlight(this._selector);
  }

  locator(selectorOrLocator: string | Locator, options?: Omit<LocatorOptions, 'visible'>): Locator {
    if (isString(selectorOrLocator))
      return new Locator(this._frame, this._selector + ' >> ' + selectorOrLocator, options);
    if (selectorOrLocator._frame !== this._frame)
      throw new Error(`Locators must belong to the same frame.`);
    return new Locator(this._frame, this._selector + ' >> internal:chain=' + JSON.stringify(selectorOrLocator._selector), options);
  }

  get(by: By): Locator {
    return this.locator(resolveBy(by, testIdAttributeName()));
  }

  getByTestId(testId: string | RegExp): Locator {
    return this.locator(getByTestIdSelector(testIdAttributeName(), testId));
  }

  getByAltText(text: string | RegExp, options?: { exact?: boolean }): Locator {
    return this.locator(getByAltTextSelector(text, options));
  }

  getByLabel(text: string | RegExp, options?: { exact?: boolean }): Locator {
    return this.locator(getByLabelSelector(text, options));
  }

View on GitHub (pinned to c8fc3bf8d3)

When it happens

Trigger: Thrown at packages/playwright-core/src/client/locator.ts:180 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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