SeleniumHQ/selenium · error · Error

You may only deselect options of a multi-select

Error message

You may only deselect options of a multi-select

What it means

Thrown by deselectByVisibleText() when the select is not a multi-select. Unlike deselectAll (error 91), this guard correctly uses `await this.isMultiple()`, so it fires reliably. The method also expects the text to match an option (it uses findElement which throws NoSuchElementError if absent). Generic Error for the multi-select check.

Source

Thrown at javascript/selenium-webdriver/lib/select.js:370

    }

    const options = await this.getOptions()

    for (let option of options) {
      if (await option.isSelected()) {
        await option.click()
      }
    }
  }

  /**
   *
   * @param {string|Number}text text of option to deselect
   * @returns {Promise<void>}
   */
  async deselectByVisibleText(text) {
    if (!(await this.isMultiple())) {
      throw new Error('You may only deselect options of a multi-select')
    }

    /**
     * convert value into string
     */
    text = typeof text === 'number' ? text.toString() : text

    const optionElement = await this.element.findElement(
      By.xpath('.//option[normalize-space(.) = ' + escapeQuotes(text) + ']'),
    )
    if (await optionElement.isSelected()) {
      await optionElement.click()
    }
  }

  /**
   *
   * @param {Number} index       index of option element to deselect

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Guard before calling: if (await sel.isMultiple()) await sel.deselectByVisibleText(t).
  2. Confirm the <select> has the multiple attribute in the page.
  3. Wrap in try/catch and treat this as a no-op for single selects if appropriate.
  4. Use a Select helper that branches on isMultiple().

Example fix

// before
await sel.deselectByVisibleText('X'); // single-select -> throws

// after
if (await sel.isMultiple()) {
  await sel.deselectByVisibleText('X');
} else {
  // single-select: deselect by picking a different/blank option
}
Defensive patterns

Strategy: validation

Validate before calling

async function safeDeselectByText(sel, text) {
  if (!(await sel.isMultiple())) throw new Error('requires multi-select');
  await sel.deselectByVisibleText(text);
}

Type guard

async function isMultiSelect(sel) {
  return Boolean(await sel.isMultiple());
}

Try / catch

try {
  await sel.deselectByVisibleText(t);
} catch (e) {
  if (/multi-select/.test(e.message)) { /* no-op for single selects */ }
  else throw e;
}

Prevention

When it happens

Trigger: Calling deselectByVisibleText on a <select> without the multiple attribute. A single-select cannot have a 'deselected' state in the multi-select sense, so the operation is rejected up front.

Common situations: Generic form helpers; test code reused across single and multi selects; assuming a dropdown is multi because it visually allows clearing.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/046aa0768b894f7e. Report an issue: GitHub.