{"record":{"id":"6e6693d21d90e39b","repo":"microsoft/playwright","slug":"error-while-parsing-selector-selector-selec","errorCode":null,"errorMessage":"Error while parsing selector `${selector}` - selector cannot be empty","messagePattern":"Error while parsing selector `(.+?)` - selector cannot be empty","errorType":"exception","errorClass":"InvalidSelectorError","httpStatus":null,"severity":"error","filePath":"packages/isomorphic/selectorParser.ts","lineNumber":465,"sourceCode":"    if (operator !== '=' && typeof value !== 'string')\n      throw new InvalidSelectorError(`Error while parsing selector \\`${selector}\\` - cannot use ${operator} in attribute with non-string matching value - ${value}`);\n    return { name: jsonPath.join('.'), jsonPath, op: operator, value, caseSensitive };\n  }\n\n  const result: AttributeSelector = {\n    name: '',\n    attributes: [],\n  };\n  result.name = readIdentifier();\n  skipSpaces();\n  while (next() === '[') {\n    result.attributes.push(readAttribute());\n    skipSpaces();\n  }\n  if (!EOL)\n    syntaxError(undefined);\n  if (!result.name && !result.attributes.length)\n    throw new InvalidSelectorError(`Error while parsing selector \\`${selector}\\` - selector cannot be empty`);\n  return result;\n}\n","sourceCodeStart":447,"sourceCodeEnd":468,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/isomorphic/selectorParser.ts#L447-L468","documentation":"Thrown by parseAttributeSelector at the end of parsing when the selector has neither a name (engine prefix) nor any attributes. It signals the input reduced to nothing meaningful, e.g. an empty string or whitespace-only input fed to the attribute selector parser.","triggerScenarios":"Calling `parseAttributeSelector('', false)`, `parseAttributeSelector('   ', false)`, or `parseAttributeSelector('[   ]', false)` where readIdentifier yields '' and no attributes are collected. Typically reached via the text/attribute engine with an empty inner expression.","commonSituations":"Dynamic selector assembly that produces an empty fragment; whitespace-only input from a config value; refactoring that left a placeholder variable empty.","solutions":["Guard the caller to reject empty/whitespace selector strings before parsing.","Provide a non-empty default or fail fast with a clearer upstream error.","Trim and validate selector input at the boundary."],"exampleFix":"// before\nconst name = process.env.LABEL ?? '';\npage.locator(`text=${name}`);  // name empty -> selector cannot be empty\n\n// after\nconst name = (process.env.LABEL ?? '').trim();\nif (!name) throw new Error('LABEL env var must be set');\npage.locator(`text=${name}`);","handlingStrategy":"validation","validationCode":"function requireNonEmptySelector(sel: string) {\n  if (typeof sel !== 'string' || sel.trim().length === 0)\n    throw new Error('Selector must be a non-empty string');\n}\nrequireNonEmptySelector(selector);","typeGuard":"function isNonEmptySelector(sel: unknown): sel is string {\n  return typeof sel === 'string' && sel.trim().length > 0;\n}","tryCatchPattern":"try {\n  await page.locator(sel).click();\n} catch (e) {\n  if (/selector cannot be empty/.test(e.message)) {\n    throw new Error('Refusing empty selector; check the source of the value');\n  }\n  throw e;\n}","preventionTips":["Trim and validate selector inputs at API boundaries.","Reject empty config values with a clearer upstream error.","Default placeholders to a safe sentinel, never an empty string."],"tags":["selector","parsing","validation","user-input"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}