microsoft/playwright · error · Error

Please use browser.newContext()

Error message

Please use browser.newContext()

What it means

Thrown by BrowserContext.newPage() when the context is owned by a page (this._ownerPage is set). A context created via page.context() is tied to that page and cannot spawn new pages; new pages must come from a fresh browser-level context. The guard `this._ownerPage` distinguishes page-owned contexts from browser-owned ones.

Source

Thrown at packages/playwright-core/src/client/browserContext.ts:314

  setDefaultTimeout(timeout: number | undefined) {
    this._timeoutSettings.setDefaultTimeout(timeout);
  }

  browser(): Browser | null {
    return this._browser;
  }

  pages(): Page[] {
    return [...this._pages];
  }

  isClosed(): boolean {
    return this._closingStatus !== 'none';
  }

  async newPage(): Promise<Page> {
    if (this._ownerPage)
      throw new Error('Please use browser.newContext()');
    return Page.from((await this._channel.newPage({}, kNoTimeout)).page);
  }

  async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
    if (!urls)
      urls = [];
    if (urls && typeof urls === 'string')
      urls = [urls];
    return (await this._channel.cookies({ urls: urls as string[] }, kNoTimeout)).cookies;
  }

  async addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void> {
    await this._channel.addCookies({ cookies }, kNoTimeout);
  }

  async clearCookies(options: network.ClearNetworkCookieOptions = {}): Promise<void> {
    await this._channel.clearCookies({
      name: isString(options.name) ? options.name : undefined,

View on GitHub (pinned to c8fc3bf8d3)

Solutions

  1. Create a new context from the browser: `const ctx = await browser.newContext(); const p = await ctx.newPage();`.
  2. If you only have a page, reach the browser via `page.context().browser()` then newContext().
  3. Avoid storing/reusing a page's context for multi-page workflows.

Example fix

// before
const ctx = page.context();
const page2 = await ctx.newPage();
// after
const browser = page.context().browser()!;
const ctx = await browser.newContext();
const page2 = await ctx.newPage();
Defensive patterns

Strategy: type-guard

Validate before calling

function canCreatePage(ctx: any): boolean {
  return !ctx._ownerPage;
}
if (!canCreatePage(context))
  throw new Error('Context is page-owned; use browser.newContext() instead.');

Type guard

function isBrowserOwnedContext(ctx: any): boolean {
  return !ctx._ownerPage;
}

Prevention

When it happens

Trigger: Calling `(await page.context()).newPage()`, or any flow that takes a page's context and tries to add pages to it. The ownerPage field is set when a context was created implicitly for a single page.

Common situations: Helper utilities that accept a Page and reuse its context to open more tabs; refactoring that accidentally passes a page-bound context where a browser-level one was expected.

Related errors


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