{"record":{"id":"58fa64b72211761e","repo":"SeleniumHQ/selenium","slug":"input-must-be-a-string","errorCode":null,"errorMessage":"input must be a string","messagePattern":"input must be a string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/by.js","lineNumber":69,"sourceCode":" */\nclass InvalidCharacterError extends Error {\n  constructor() {\n    super()\n    this.name = this.constructor.name\n  }\n}\n\n/**\n * Escapes a CSS string.\n * @param {string} css the string to escape.\n * @return {string} the escaped string.\n * @throws {TypeError} if the input value is not a string.\n * @throws {InvalidCharacterError} if the string contains an invalid character.\n * @see https://drafts.csswg.org/cssom/#serialize-an-identifier\n */\nfunction escapeCss(css) {\n  if (typeof css !== 'string') {\n    throw new TypeError('input must be a string')\n  }\n  let ret = ''\n  const n = css.length\n  for (let i = 0; i < n; i++) {\n    const c = css.charCodeAt(i)\n    if (c == 0x0) {\n      throw new InvalidCharacterError()\n    }\n\n    if (\n      (c >= 0x0001 && c <= 0x001f) ||\n      c == 0x007f ||\n      (i == 0 && c >= 0x0030 && c <= 0x0039) ||\n      (i == 1 && c >= 0x0030 && c <= 0x0039 && css.charCodeAt(0) == 0x002d)\n    ) {\n      ret += '\\\\' + c.toString(16) + ' '\n      continue\n    }","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/by.js#L51-L87","documentation":"Thrown by escapeCss() in by.js when the input typeof is not 'string'. This function serializes a CSS identifier per the CSSOM spec and requires a string input. It is used internally by By.css() and available as a public export.","triggerScenarios":"Passing a number (e.g., element.id that is numeric), null, undefined, an object, or a boolean to escapeCss(). Dynamically building a CSS selector with a non-string fragment.","commonSituations":"Element IDs read from attributes or data that are numbers; null values from failed lookups passed into selector construction; passing a DOM element instead of its ID string.","solutions":["Convert the value to a string first: escapeCss(String(value)).","Validate typeof value === 'string' before calling.","Use template literals to ensure string context: escapeCss(`${value}`)."],"exampleFix":"// before\nBy.css('#' + escapeCss(element.id)) // element.id is a number\n// after\nBy.css('#' + escapeCss(String(element.id)))","handlingStrategy":"type-guard","validationCode":"if (typeof css !== 'string') {\n  throw new TypeError(`escapeCss expects a string, got ${typeof css}`)\n}","typeGuard":"/**\n * @param {*} v\n * @returns {v is string}\n */\nfunction isString(v) {\n  return typeof v === 'string'\n}","tryCatchPattern":null,"preventionTips":["Coerce values with String() before passing to escapeCss().","Validate typeof before building CSS selectors dynamically.","Be cautious with numeric IDs — convert them explicitly."],"tags":["css","validation","locator","type","by"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}