{
  "slug": "playwright-locator-scoping-with-locator-within-98f84407f1b7",
  "title": "Playwright locator scoping with `locator.within()`",
  "tools": [
    "playwright"
  ],
  "tags": [
    "agent"
  ],
  "introduced_at": "2026-10-07T20:40:43.000Z",
  "announced_at": "2026-10-07T20:40:43.000Z",
  "effective_at": null,
  "date_basis": "source-publication",
  "status": "unknown",
  "how": {
    "incantation": "When an element must be located relative to a dialog or separately within each matching parent, combine existing locators with `locator.within()` and perform the action on the result.",
    "examples": [
      "const saveButton = page.getByRole('button', { name: 'Save' });\nconst dialog = page.getByTestId('settings-dialog');\nawait saveButton.within(dialog).click();",
      "const thirdColumn = page.getByRole('cell').nth(2).within(page.getByRole('row'));\nawait expect(thirdColumn).toHaveText(['Apple', 'Banana', 'Cherry']);"
    ],
    "gotchas": [
      "Version: v1.64.0",
      "locator.within(locator) (required): Returns a locator matching this locator's elements inside each element matched by the argument locator.",
      "locator.nth(index): Returns the zero-based n-th matching element; with `within()`, it resolves separately inside each matched parent.",
      "getByRole(role, options): Creates a locator by ARIA role; the example uses the `button` role and `name: 'Save'`.",
      "getByTestId(testId): Creates a locator by test id; the example uses `settings-dialog`.",
      "Prerequisite: Use Playwright v1.64 or later; `locator.within` was added in v1.64."
    ]
  },
  "why_it_matters": "Locate and act on an element relative to a dialog or independently within each matching parent.",
  "sources": [
    "https://github.com/microsoft/playwright/releases/tag/v1.64.0",
    "https://playwright.dev/docs/api/class-locator#locator-within",
    "https://playwright.dev/docs/api/class-locator#locator-nth",
    "https://playwright.dev/docs/api/class-locator"
  ],
  "rank": 0.99056,
  "judge": {
    "publish": true,
    "judge_model": "codex-cli/gpt-6-luna+typesafe/jev-1.13.0",
    "prompt_version": "extract-v7+enrich-v1+judge-v2x3+jev-publish-v1"
  },
  "review_state": "approved",
  "provenance": {
    "run_id": "gap-20261007",
    "verdict_id": "gap-20261007:gap-20261007-bdf5b973c35fe6e6-6:gate",
    "evidence_hash": "d55d809be2ea27eaa2466c245352f93486b5b4d563d8e70bca21767e52f93303"
  },
  "review": {
    "draft_hash": "2e3ae9c8499a77e6ca1dcdde5992a2135779fe1cdcf693a457d7d36940e01d23",
    "reviewer": "publication-gate",
    "reviewed_at": "2026-10-07T20:40:43.000Z",
    "decision": "gap-20261007:gate",
    "reason": "3 of 3 judge runs approved with p_yes >= 0.9 (mean 0.99 >= 0.9; 2 needed) and Jev gave 0.74 (>= 0.6 needed)."
  }
}
