{"record":{"id":"3f6f16f6fee05bd9","repo":"microsoft/playwright","slug":"target-element-must-belong-to-the-root-s-subtree","errorCode":null,"errorMessage":"Target element must belong to the root's subtree","messagePattern":"Target element must belong to the root's subtree","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/injected/src/selectorGenerator.ts","lineNumber":123,"sourceCode":"    }\n    const selector = joinTokens(targetTokens);\n    const parsedSelector = injectedScript.parseSelector(selector);\n    return {\n      selector,\n      elements: injectedScript.querySelectorAll(parsedSelector, options.root ?? targetElement.ownerDocument)\n    };\n  } finally {\n    endDOMCaches();\n    endAriaCaches();\n    injectedScript._evaluator.end();\n  }\n}\n\ntype InternalOptions = GenerateSelectorOptions & { isRecursive?: boolean };\n\nfunction generateSelectorFor(cache: Cache, injectedScript: InjectedScript, targetElement: Element, options: InternalOptions): SelectorToken[] | null {\n  if (options.root && !isInsideScope(options.root, targetElement))\n    throw new Error(`Target element must belong to the root's subtree`);\n\n  if (targetElement === options.root)\n    return [{ engine: 'css', selector: ':scope', score: 1 }];\n  if (targetElement.ownerDocument.documentElement === targetElement)\n    return [{ engine: 'css', selector: 'html', score: 1 }];\n\n  let result: SelectorToken[] | null = null;\n  const updateResult = (candidate: SelectorToken[]) => {\n    if (!result || combineScores(candidate) < combineScores(result))\n      result = candidate;\n  };\n\n  const candidates: { candidate: SelectorToken[], isTextCandidate: boolean }[] = [];\n  for (const candidate of buildTextCandidates(injectedScript, targetElement, !options.isRecursive, options))\n    candidates.push({ candidate, isTextCandidate: true });\n  for (const token of buildNoTextCandidates(injectedScript, targetElement, options)) {\n    if (options.omitInternalEngines && token.engine.startsWith('internal:'))\n      continue;","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/injected/src/selectorGenerator.ts#L105-L141","documentation":"Error \"Target element must belong to the root's subtree\" thrown in microsoft/playwright.","triggerScenarios":"Thrown at packages/injected/src/selectorGenerator.ts:123 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}