{"record":{"id":"bd1fdd69f642be42","repo":"SeleniumHQ/selenium","slug":"could-not-locate-element-with-visible-text-text","errorCode":null,"errorMessage":"Could not locate element with visible text: {text}","messagePattern":"Could not locate element with visible text: (.+?)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/select.py","lineNumber":145,"sourceCode":"\n        if len(opts) == 0 and \" \" in text:\n            sub_string_without_space = self._get_longest_token(text)\n            if sub_string_without_space == \"\":\n                candidates = self.options\n            else:\n                xpath = f\".//option[contains(.,{self._escape_string(sub_string_without_space)})]\"\n                candidates = self._el.find_elements(By.XPATH, xpath)\n            for candidate in candidates:\n                if text == candidate.text:\n                    if not self._has_css_property_and_visible(candidate):\n                        raise NoSuchElementException(f\"Invisible option with text: {text}\")\n                    self._set_selected(candidate)\n                    if not self.is_multiple:\n                        return\n                    matched = True\n\n        if not matched:\n            raise NoSuchElementException(f\"Could not locate element with visible text: {text}\")\n\n    def deselect_all(self) -> None:\n        \"\"\"Clear all selected entries.\n\n        This is only valid when the SELECT supports multiple selections.\n        throws NotImplementedError If the SELECT does not support\n        multiple selections\n        \"\"\"\n        if not self.is_multiple:\n            raise NotImplementedError(\"You may only deselect all options of a multi-select\")\n        for opt in self.options:\n            self._unset_selected(opt)\n\n    def deselect_by_value(self, value: str) -> None:\n        \"\"\"Deselect all options that have a value matching the argument.\n\n        Example:\n            When given \"foo\" this would deselect an option like:","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/select.py#L127-L163","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["List actual option texts first: `[o.text for o in select.options]` and compare","Switch to `select_by_value` if a stable value attribute exists","Strip/normalise whitespace in the input text before passing","Add an explicit wait until an option with that text appears"],"exampleFix":"// before\nselect.select_by_visible_text(\"UnitedStates\")\n\n// after\ntexts = [o.text for o in select.options]\ntarget = \"United States\"\nif target in texts:\n    select.select_by_visible_text(target)","handlingStrategy":"try-catch","validationCode":"texts = [o.text for o in select.options]\nif text in texts:\n    select.select_by_visible_text(text)","typeGuard":"def text_exists(select_obj, text) -> bool:\n    return text in [o.text for o in select_obj.options]","tryCatchPattern":"from selenium.common.exceptions import NoSuchElementException\n\ntry:\n    select.select_by_visible_text(text)\nexcept NoSuchElementException:\n    # no such visible text — handle\n    pass","preventionTips":["Snapshot option texts from external data before selecting","Normalise whitespace in input text","Use select_by_value when labels are localised/variable"],"tags":["select","element-not-found","selenium-python"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}