SeleniumHQ/selenium · error · NoSuchElementException

Could not locate element with value: {value}

Error message

Could not locate element with value: {value}

What it means

`deselect_by_value()` builds `option[value=...]`, deselects matches, and if none matched raises `NoSuchElementException('Could not locate element with value: ...')`. The value simply does not exist among the (multi-select's) options.

Source

Thrown at py/selenium/webdriver/support/select.py:182

                `<option value="foo">Bar</option>`

        Args:
            value: The value to match against

        Raises:
            NoSuchElementException: If there is no option with specified value in SELECT
        """
        if not self.is_multiple:
            raise NotImplementedError("You may only deselect options of a multi-select")
        matched = False
        css = f"option[value = {self._escape_string(value)}]"
        opts = self._el.find_elements(By.CSS_SELECTOR, css)
        for opt in opts:
            self._unset_selected(opt)
            matched = True
        if not matched:
            raise NoSuchElementException(f"Could not locate element with value: {value}")

    def deselect_by_index(self, index: int) -> None:
        """Deselect the option at the given index by examining the "index" attribute.

        Args:
            index: The option at this index will be deselected

        Raises:
            NoSuchElementException: If there is no option with specified index in SELECT
        """
        if not self.is_multiple:
            raise NotImplementedError("You may only deselect options of a multi-select")
        for opt in self.options:
            if opt.get_attribute("index") == str(index):
                self._unset_selected(opt)
                return
        raise NoSuchElementException(f"Could not locate element with index {index}")

View on GitHub (pinned to aa36b38e69)

Solutions

  1. List option values first: `[o.get_attribute('value') for o in select.options]`
  2. Confirm the value string matches exactly
  3. Catch NoSuchElementException and treat as 'already deselected / nothing to do'

Example fix

// before
select.deselect_by_value("USA")

// after
values = [o.get_attribute("value") for o in select.options]
if "us" in values:
    select.deselect_by_value("us")
Defensive patterns

Strategy: try-catch

Validate before calling

values = [o.get_attribute("value") for o in select.options]
if value in values:
    select.deselect_by_value(value)

Type guard

def value_exists(select_obj, value) -> bool:
    return value in [o.get_attribute("value") for o in select_obj.options]

Try / catch

from selenium.common.exceptions import NoSuchElementException

try:
    select.deselect_by_value(value)
except NoSuchElementException:
    # nothing to deselect
    pass

Prevention

When it happens

Trigger: Calling `select.deselect_by_value('foo')` on a multi-select where no `<option value="foo">` exists. The matched loop never sets `matched=True`.

Common situations: Stale config-driven values; case/whitespace mismatch; the option was removed dynamically; value belongs to a different select.

Related errors


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