From c52e0bc999f2fb41e2090e17b5099e164637d06b Mon Sep 17 00:00:00 2001 From: Roushan7970835758 Date: Sun, 10 Aug 2025 00:58:36 +0530 Subject: [PATCH] fixed 15265-Unifying Select Class Across All Bindings --- .../org/openqa/selenium/support/ui/Select.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/java/src/org/openqa/selenium/support/ui/Select.java b/java/src/org/openqa/selenium/support/ui/Select.java index 264585e5417f6..39df5e50c3b9d 100644 --- a/java/src/org/openqa/selenium/support/ui/Select.java +++ b/java/src/org/openqa/selenium/support/ui/Select.java @@ -123,13 +123,15 @@ public WebElement getFirstSelectedOption() { @Override public void selectByVisibleText(String text) { assertSelectIsEnabled(); - + assertSelectIsVisible(); // try to find the option via XPATH ... List options = element.findElements( By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]")); for (WebElement option : options) { + if (!hasCssPropertyAndVisible(option)) + throw new NoSuchElementException("Invisible option with text: " + text); setSelected(option, true); if (!isMultiple()) { return; @@ -137,17 +139,18 @@ public void selectByVisibleText(String text) { } boolean matched = !options.isEmpty(); - if (!matched && text.contains(" ")) { - String subStringWithoutSpace = getLongestSubstringWithoutSpace(text); - List candidates; - if ("".equals(subStringWithoutSpace)) { + if (!matched) { + String searchText = text.contains(" ") ? getLongestSubstringWithoutSpace(text) : text; + List candidates; + + if (searchText.isEmpty()) { // hmm, text is either empty or contains only spaces - get all options ... candidates = element.findElements(By.tagName("option")); } else { // get candidates via XPATH ... candidates = element.findElements( - By.xpath(".//option[contains(., " + Quotes.escape(subStringWithoutSpace) + ")]")); + By.xpath(".//option[contains(., " + Quotes.escape(searchText) + ")]")); } String trimmed = text.trim();