{"record":{"id":"35b1c6b9c1a425e4","repo":"SeleniumHQ/selenium","slug":"select-only-works-on-select-elements","errorCode":null,"errorMessage":"Select only works on <select> elements","messagePattern":"Select only works on <select> elements","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/select.js","lineNumber":157,"sourceCode":"\n/**\n * @implements ISelect\n */\nclass Select {\n  /**\n   * Create an Select Element\n   * @param {WebElement} element Select WebElement.\n   */\n  constructor(element) {\n    if (element === null) {\n      throw new Error(`Element must not be null. Please provide a valid <select> element.`)\n    }\n\n    this.element = element\n\n    this.element.getAttribute('tagName').then(function (tagName) {\n      if (tagName.toLowerCase() !== 'select') {\n        throw new Error(`Select only works on <select> elements`)\n      }\n    })\n\n    this.element.getAttribute('multiple').then((multiple) => {\n      this.multiple = multiple !== null && multiple !== 'false'\n    })\n  }\n\n  /**\n   *\n   * Select option with specified index.\n   *\n   * <example>\n   <select id=\"selectbox\">\n   <option value=\"1\">Option 1</option>\n   <option value=\"2\">Option 2</option>\n   <option value=\"3\">Option 3</option>\n   </select>","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/select.js#L139-L175","documentation":"Thrown inside a `.then()` callback in the Select constructor when the element's tagName is not 'select'. IMPORTANT: the throw occurs inside an un-awaited promise — the constructor does not return or await it, so the rejection becomes an unhandled promise rejection (or surfaces later, detached from the call site). Users may see a confusing async error rather than a clean constructor failure.","triggerScenarios":"new Select(divElement); new Select(textareaElement); new Select(inputElement); any element whose tagName.toLowerCase() !== 'select'.","commonSituations":"Locating an element by an id that exists but is a different tag; generic form helpers that wrap any element in Select; copied locators that match the wrong element after a DOM refactor.","solutions":["Verify the tag before constructing: const tag = await el.getTagName(); if (tag !== 'select') throw new Error('not a select');","Use a locator scoped to selects: By.css('select#myid') so the wrong tag cannot match.","Because the constructor's check is async and detached, perform your own synchronous-able pre-check and don't rely on the constructor to validate.","Register a global unhandledRejection handler so a missed check surfaces loudly during dev."],"exampleFix":"// before\nconst el = await driver.findElement(By.id('qty')); // a <div id=\"qty\">\nconst sel = new Select(el); // async unhandled rejection\n\n// after\nconst el = await driver.findElement(By.css('select#qty'));\n// or guard:\nif ((await el.getTagName()) !== 'select') throw new Error('element is not a <select>');\nconst sel = new Select(el);","handlingStrategy":"validation","validationCode":"async function safeSelect(el) {\n  const tag = (await el.getTagName()).toLowerCase();\n  if (tag !== 'select') {\n    throw new Error(`expected <select>, got <${tag}>`);\n  }\n  return new Select(el);\n}","typeGuard":"async function isSelectElement(el) {\n  return (await el.getTagName()).toLowerCase() === 'select';\n}","tryCatchPattern":"process.on('unhandledRejection', (reason) => {\n  if (reason instanceof Error && /Select only works on/.test(reason.message)) {\n    log.error('Select created on a non-select element');\n  }\n});\n// Prefer pre-validation rather than catching this detached rejection.","preventionTips":["Scope locators to selects: By.css('select#id') so wrong tags cannot match.","Pre-check getTagName() because the constructor's check is async/detached.","Add a global unhandledRejection handler in dev to surface detached throws."],"tags":["select","element-validation","async","unhandled-rejection","constructor"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}