{"record":{"id":"6bd1752f7f813cc4","repo":"SeleniumHQ/selenium","slug":"invalid-locator","errorCode":null,"errorMessage":"Invalid locator","messagePattern":"Invalid locator","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/by.js","lineNumber":469,"sourceCode":"  if (locator instanceof By || locator instanceof RelativeBy || typeof locator === 'function') {\n    return locator\n  }\n\n  if (\n    locator &&\n    typeof locator === 'object' &&\n    typeof locator.using === 'string' &&\n    typeof locator.value === 'string'\n  ) {\n    return new By(locator.using, locator.value)\n  }\n\n  for (let key in locator) {\n    if (Object.prototype.hasOwnProperty.call(locator, key) && Object.prototype.hasOwnProperty.call(By, key)) {\n      return By[key](locator[key])\n    }\n  }\n  throw new TypeError('Invalid locator')\n}\n\n// PUBLIC API\n\nmodule.exports = {\n  By,\n  RelativeBy,\n  withTagName,\n  locateWith,\n  escapeCss,\n  checkedLocator: check,\n}\n","sourceCodeStart":451,"sourceCodeEnd":482,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/by.js#L451-L482","documentation":"Thrown by checkedLocator() (exported as checkedLocator) in by.js when the input cannot be resolved into a valid By or RelativeBy. The function checks for By/RelativeBy instances, functions, hash objects with using/value string fields, and objects whose keys match By static methods. If none match, this TypeError fires.","triggerScenarios":"Passing null, undefined, an empty object, or a plain object whose keys don't match any By method (e.g., {csss:'#foo'}). Passing a number or string directly instead of a locator object.","commonSituations":"Dynamically constructing locators from config or untrusted data; typos in By hash keys; passing a raw selector string where an object is expected; passing an object with custom keys.","solutions":["Use explicit By factories: By.css('#id'), By.xpath('//div'), By.id('id').","Verify locator hash keys match By method names exactly (css, xpath, id, className, name, tagName, linkText, partialLinkText).","Check for null/undefined before passing to findElement/findElements.","Use the By constant directly rather than a hash object."],"exampleFix":"// before\ndriver.findElement({ csss: '#foo' })\n// after\ndriver.findElement({ css: '#foo' })\n// or even better\ndriver.findElement(By.css('#foo'))","handlingStrategy":"type-guard","validationCode":"const { By } = require('selenium-webdriver')\nconst VALID_BY_KEYS = new Set(['css', 'className', 'id', 'name', 'tagName', 'linkText', 'partialLinkText', 'xpath', 'js'])\nfunction isValidLocatorHash(obj) {\n  return Object.keys(obj).some((k) => VALID_BY_KEYS.has(k))\n}\nif (locator == null || (typeof locator === 'object' && !isValidLocatorHash(locator))) {\n  throw new TypeError('Invalid locator: use By.css(), By.xpath(), etc.')\n}","typeGuard":"/**\n * @param {*} l\n * @returns {boolean}\n */\nfunction isValidLocator(l) {\n  if (l instanceof By || l instanceof RelativeBy || typeof l === 'function') return true\n  if (l && typeof l === 'object') {\n    if (typeof l.using === 'string' && typeof l.value === 'string') return true\n    return Object.keys(l).some((k) => typeof By[k] === 'function')\n  }\n  return false\n}","tryCatchPattern":null,"preventionTips":["Prefer explicit By.css(), By.xpath() over hash objects.","When using hash locators from config, validate keys against the By method names.","Check for null/undefined before calling findElement."],"tags":["locator","validation","css","xpath","by"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}