diff --git a/java/src/org/openqa/selenium/support/ui/Select.java b/java/src/org/openqa/selenium/support/ui/Select.java index 264585e5417f6..1e9a01e289fdd 100644 --- a/java/src/org/openqa/selenium/support/ui/Select.java +++ b/java/src/org/openqa/selenium/support/ui/Select.java @@ -123,19 +123,31 @@ 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) + "]")); + boolean selectedAnyVisible = false; + for (WebElement option : options) { - setSelected(option, true); - if (!isMultiple()) { - return; + if (hasCssPropertyAndVisible(option)) { + // throw new NoSuchElementException("Invisible option with text: " + text); + setSelected(option, true); + selectedAnyVisible = true; + if (!isMultiple()) { + return; + } } } + if (!selectedAnyVisible && !options.isEmpty()) { + // if we found options, but none of them was visible, we throw an exception + throw new NoSuchElementException("No visible option with text: " + text); + } + boolean matched = !options.isEmpty(); if (!matched && text.contains(" ")) { String subStringWithoutSpace = getLongestSubstringWithoutSpace(text);