MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / select_by_visible_text

Method select_by_visible_text

py/selenium/webdriver/support/select.py:103–145  ·  view source on GitHub ↗

Select all options that display text matching the argument. Example: When given "Bar" this would select an option like: ` Bar ` Args: text: The visible text to match against Raises: NoSuchElementEx

(self, text: str)

Source from the content-addressed store, hash-verified

101 raise NoSuchElementException(f"Could not locate element with index {index}")
102
103 def select_by_visible_text(self, text: str) -> None:
104 """Select all options that display text matching the argument.
105
106 Example:
107 When given "Bar" this would select an option like:
108
109 `<option value="foo">Bar</option>`
110
111 Args:
112 text: The visible text to match against
113
114 Raises:
115 NoSuchElementException: If there is no option with specified text in SELECT
116 """
117 xpath = f".//option[normalize-space(.) = {self._escape_string(text)}]"
118 opts = self._el.find_elements(By.XPATH, xpath)
119 matched = False
120 for opt in opts:
121 if not self._has_css_property_and_visible(opt):
122 raise NoSuchElementException(f"Invisible option with text: {text}")
123 self._set_selected(opt)
124 if not self.is_multiple:
125 return
126 matched = True
127
128 if len(opts) == 0 and " " in text:
129 sub_string_without_space = self._get_longest_token(text)
130 if sub_string_without_space == "":
131 candidates = self.options
132 else:
133 xpath = f".//option[contains(.,{self._escape_string(sub_string_without_space)})]"
134 candidates = self._el.find_elements(By.XPATH, xpath)
135 for candidate in candidates:
136 if text == candidate.text:
137 if not self._has_css_property_and_visible(candidate):
138 raise NoSuchElementException(f"Invisible option with text: {text}")
139 self._set_selected(candidate)
140 if not self.is_multiple:
141 return
142 matched = True
143
144 if not matched:
145 raise NoSuchElementException(f"Could not locate element with visible text: {text}")
146
147 def deselect_all(self) -> None:
148 """Clear all selected entries.

Calls 6

_escape_stringMethod · 0.95
_set_selectedMethod · 0.95
_get_longest_tokenMethod · 0.95
find_elementsMethod · 0.45