{"record":{"id":"0a10922583faa24d","repo":"didi/DoKit","slug":"can-t-generate-css-selector-for-non-element-node-t","errorCode":null,"errorMessage":"Can't generate CSS selector for non-element node type.","messagePattern":"Can't generate CSS selector for non-element node type\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Web/packages/core/src/common/js/finder/index.js","lineNumber":13,"sourceCode":"import cssesc from 'cssesc';\n\nlet Limit;\n(function (Limit) {\n  Limit[Limit.All = 0] = 'All';\n  Limit[Limit.Two = 1] = 'Two';\n  Limit[Limit.One = 2] = 'One';\n}(Limit || (Limit = {})));\nlet config;\nlet rootDocument;\nexport default function (input, options) {\n  if (input.nodeType !== Node.ELEMENT_NODE) {\n    throw new Error('Can\\'t generate CSS selector for non-element node type.');\n  }\n  if (input.tagName.toLowerCase() === 'html') {\n    return 'html';\n  }\n  const defaults = {\n    root: document.body,\n    idName: name => true,\n    className: (name, input) => true,\n    tagName: name => true,\n    attr: (name, value) => false,\n    seedMinLength: 1,\n    optimizedMinLength: 2,\n    threshold: 1000,\n  };\n  config = Object.assign({}, defaults, options);\n  rootDocument = findRootDocument(config.root, defaults);\n  let path = bottomUpSearch(input, Limit.All, () => bottomUpSearch(input, Limit.Two, () => bottomUpSearch(input, Limit.One)));\n  // console.log('bottomUpSearch:', path)","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Web/packages/core/src/common/js/finder/index.js#L1-L31","documentation":"Thrown by the CSS selector generator (a fork of the 'finder' library) when the node passed in is not an ELEMENT_NODE. The library builds a unique CSS selector by walking up the DOM from an element, so it can only operate on element nodes. Passing a text node, comment node, document, or document fragment triggers this immediately at the entry check.","triggerScenarios":"Calling the default export of finder/index.js with: a text node (nodeType 3) obtained via firstChild/childNodes[i], a comment node (nodeType 8), document (nodeType 9), documentFragment (nodeType 11), or the result of a DOM API that returns null/undefined coerced through a non-element accessor. Also happens when event.target is a text node (e.g. from a non-element context) and is fed directly to the finder.","commonSituations":"Recording user clicks for session replay or element-picker features: e.target is usually an element but can be a text node in some browsers/edge cases. Accessing parentNode of an element can return a document node. Using childNodes (returns all node types) instead of children (elements only).","solutions":["Check node.nodeType === Node.ELEMENT_NODE (or node.nodeType === 1) before calling the finder","Use element.children / parentElement instead of childNodes / parentNode when you need element-only traversal","If the node is a text/comment node, pass its parentElement instead: node.parentElement","Guard with instanceof: node instanceof Element (or the appropriate DOM class) before generating the selector"],"exampleFix":"// before\nconst selector = finder(node, { root: document.body });\n\n// after\nif (node && node.nodeType === Node.ELEMENT_NODE) {\n  const selector = finder(node, { root: document.body });\n} else if (node && node.parentElement) {\n  const selector = finder(node.parentElement, { root: document.body });\n}","handlingStrategy":"type-guard","validationCode":"const isElement = (n) => n && n.nodeType === Node.ELEMENT_NODE;\nif (!isElement(node)) node = node && node.parentElement;\nif (isElement(node)) selector = finder(node, { root: document.body });","typeGuard":"function isElementNode(node) {\n  return node instanceof Element;\n}","tryCatchPattern":null,"preventionTips":["Prefer element.children and parentElement over childNodes and parentNode for element-only traversal","Always narrow event.target / captured nodes with nodeType === 1 before selector generation"],"tags":["dom","css-selector","javascript","type-guard"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}