Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions common/src/web/formPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@

<select id="invisible_multi_select" multiple>
<option selected="selected" value="apples" style="opacity: 0;">Apples</option>
<option value="oranges">Oranges</option>
<option selected="selected" value="lemons">Lemons</option>
<option value="pears" style="opacity: 0.0;">Pears</option>
<option value="oranges" style="display: none;">Oranges</option>
<option selected="selected" value="lemons" style="visibility: hidden;">Lemons</option>
</select>

<select name="select-default">
Expand Down
16 changes: 16 additions & 0 deletions py/selenium/webdriver/support/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def select_by_visible_text(self, text: str) -> None:
opts = self._el.find_elements(By.XPATH, xpath)
matched = False
for opt in opts:
if not self._has_css_property_and_visible(opt):
raise NoSuchElementException(f"Invisible option with text: {text}")
self._set_selected(opt)
if not self.is_multiple:
return
Expand All @@ -128,6 +130,8 @@ def select_by_visible_text(self, text: str) -> None:
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
Expand Down Expand Up @@ -202,6 +206,8 @@ def deselect_by_visible_text(self, text: str) -> None:
xpath = f".//option[normalize-space(.) = {self._escape_string(text)}]"
opts = self._el.find_elements(By.XPATH, xpath)
for opt in opts:
if not self._has_css_property_and_visible(opt):
raise NoSuchElementException(f"Invisible option with text: {text}")
self._unset_selected(opt)
matched = True
if not matched:
Expand Down Expand Up @@ -241,3 +247,13 @@ def _get_longest_token(self, value: str) -> str:
if len(item) > len(longest):
longest = item
return longest

def _has_css_property_and_visible(self, option) -> bool:
css_value_candidates = ["hidden", "none", "0", "0.0"]
css_property_candidates = ["visibility", "display", "opacity"]

for property in css_property_candidates:
css_value = option.value_of_css_property(property)
if css_value in css_value_candidates:
return False
return True
10 changes: 10 additions & 0 deletions py/test/selenium/webdriver/common/select_class_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
disabledSelect = {"name": "no-select", "values": ["Foo"]}
disabledSingleSelect = {"name": "single_disabled", "values": ["Enabled", "Disabled"]}
disabledMultiSelect = {"name": "multi_disabled", "values": ["Enabled", "Disabled"]}
invisibleMultiSelect = {"id": "invisible_multi_select", "values": ["Apples", "Pears", "Oranges", "Lemons"]}
singleSelectValues1 = {
"name": "selectomatic",
"values": ["One", "Two", "Four", "Still learning how to count, apparently"],
Expand Down Expand Up @@ -161,6 +162,15 @@ def test_raises_exception_select_by_text_multiple_disabled(driver, pages):
sel.select_by_visible_text(disabledMultiSelect["values"][1])


def test_raises_exception_select_by_text_multiple_hidden(driver, pages):
pages.load("formPage.html")

sel = Select(driver.find_element(By.ID, invisibleMultiSelect["id"]))
for option in invisibleMultiSelect["values"]:
with pytest.raises(NoSuchElementException):
sel.select_by_visible_text(option)


def test_deselect_all_single(driver, pages):
pages.load("formPage.html")
for select in [singleSelectValues1, singleSelectValues2]:
Expand Down
Loading