SeleniumHQ/selenium · error · NoSuchElementException

Could not locate element with visible text: {text}

Error message

Could not locate element with visible text: {text}

What it means

`select_by_visible_text()` raises `NoSuchElementException('Could not locate element with visible text: ...')` when no option matched the text at all — neither by normalised XPath nor, when the text has a space, by the longest-token fallback. It is the terminal 'nothing found' raise of the method.

Source

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

        if len(opts) == 0 and " " in text:
            sub_string_without_space = self._get_longest_token(text)
            if sub_string_without_space == "":
                candidates = self.options
            else:
                xpath = f".//option[contains(.,{self._escape_string(sub_string_without_space)})]"
                candidates = self._el.find_elements(By.XPATH, xpath)
            for candidate in candidates:
                if text == candidate.text:
                    if not self._has_css_property_and_visible(candidate):
                        raise NoSuchElementException(f"Invisible option with text: {text}")
                    self._set_selected(candidate)
                    if not self.is_multiple:
                        return
                    matched = True

        if not matched:
            raise NoSuchElementException(f"Could not locate element with visible text: {text}")

    def deselect_all(self) -> None:
        """Clear all selected entries.

        This is only valid when the SELECT supports multiple selections.
        throws NotImplementedError If the SELECT does not support
        multiple selections
        """
        if not self.is_multiple:
            raise NotImplementedError("You may only deselect all options of a multi-select")
        for opt in self.options:
            self._unset_selected(opt)

    def deselect_by_value(self, value: str) -> None:
        """Deselect all options that have a value matching the argument.

        Example:
            When given "foo" this would deselect an option like:

View on GitHub (pinned to aa36b38e69)

Solutions

  1. List actual option texts first: `[o.text for o in select.options]` and compare
  2. Switch to `select_by_value` if a stable value attribute exists
  3. Strip/normalise whitespace in the input text before passing
  4. Add an explicit wait until an option with that text appears

Example fix

// before
select.select_by_visible_text("UnitedStates")

// after
texts = [o.text for o in select.options]
target = "United States"
if target in texts:
    select.select_by_visible_text(target)
Defensive patterns

Strategy: try-catch

Validate before calling

texts = [o.text for o in select.options]
if text in texts:
    select.select_by_visible_text(text)

Type guard

def text_exists(select_obj, text) -> bool:
    return text in [o.text for o in select_obj.options]

Try / catch

from selenium.common.exceptions import NoSuchElementException

try:
    select.select_by_visible_text(text)
except NoSuchElementException:
    # no such visible text — handle
    pass

Prevention

When it happens

Trigger: Calling `select.select_by_visible_text('Bar')` when no option displays that text. The XPath normalised match returns nothing and (for spaced text) the token fallback also yields no candidate whose `.text` equals the target.

Common situations: Stale test data; the option text changed after a UI update; locale/i18n differences mean the visible label differs; whitespace or unicode normalisation mismatch; the option is in a different select.

Related errors


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