Skip to content

Commit 38f35ea

Browse files
committed
[py] Fix select being able to select options hidden by css rules
1 parent 445ba70 commit 38f35ea

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

py/selenium/webdriver/support/select.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def select_by_visible_text(self, text: str) -> None:
114114
opts = self._el.find_elements(By.XPATH, xpath)
115115
matched = False
116116
for opt in opts:
117+
if not self._has_css_property_and_visible(opt):
118+
raise NoSuchElementException(f"Could not locate element with visible text: {text}")
117119
self._set_selected(opt)
118120
if not self.is_multiple:
119121
return
@@ -202,6 +204,8 @@ def deselect_by_visible_text(self, text: str) -> None:
202204
xpath = f".//option[normalize-space(.) = {self._escape_string(text)}]"
203205
opts = self._el.find_elements(By.XPATH, xpath)
204206
for opt in opts:
207+
if not self._has_css_property_and_visible(opt):
208+
raise NoSuchElementException(f"Could not locate element with visible text: {text}")
205209
self._unset_selected(opt)
206210
matched = True
207211
if not matched:
@@ -241,3 +245,14 @@ def _get_longest_token(self, value: str) -> str:
241245
if len(item) > len(longest):
242246
longest = item
243247
return longest
248+
249+
def _has_css_property_and_visible(self, option) -> bool:
250+
css_value_candidates = ["hidden", "none", "0", "0.0"]
251+
css_property_candidates = ["visibility", "display", "opacity"]
252+
253+
for property in css_property_candidates:
254+
css_value = option.value_of_css_property(property)
255+
if css_value in css_value_candidates:
256+
return False
257+
return True
258+

py/test/selenium/webdriver/common/select_class_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
disabledSelect = {"name": "no-select", "values": ["Foo"]}
2626
disabledSingleSelect = {"name": "single_disabled", "values": ["Enabled", "Disabled"]}
2727
disabledMultiSelect = {"name": "multi_disabled", "values": ["Enabled", "Disabled"]}
28+
invisibleMultiSelect = {"id": "invisible_multi_select", "values": ["Apples", "Oranges", "Lemons"]}
2829
singleSelectValues1 = {
2930
"name": "selectomatic",
3031
"values": ["One", "Two", "Four", "Still learning how to count, apparently"],
@@ -160,6 +161,12 @@ def test_raises_exception_select_by_text_multiple_disabled(driver, pages):
160161
with pytest.raises(NotImplementedError):
161162
sel.select_by_visible_text(disabledMultiSelect["values"][1])
162163

164+
def test_raises_exception_select_by_text_multiple_hidden(driver, pages):
165+
pages.load("formPage.html")
166+
167+
sel = Select(driver.find_element(By.ID, invisibleMultiSelect["id"]))
168+
with pytest.raises(NoSuchElementException):
169+
sel.select_by_visible_text(invisibleMultiSelect["values"][0])
163170

164171
def test_deselect_all_single(driver, pages):
165172
pages.load("formPage.html")

0 commit comments

Comments
 (0)