Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py] Fix select being able to select options hidden by css rules #15135

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
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
9 changes: 9 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", "Oranges", "Lemons"]}
singleSelectValues1 = {
"name": "selectomatic",
"values": ["One", "Two", "Four", "Still learning how to count, apparently"],
Expand Down Expand Up @@ -161,6 +162,14 @@ 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"]))
with pytest.raises(NoSuchElementException):
sel.select_by_visible_text(invisibleMultiSelect["values"][0])


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