Skip to content
Closed
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
15 changes: 9 additions & 6 deletions java/src/org/openqa/selenium/support/ui/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,31 +123,34 @@ public WebElement getFirstSelectedOption() {
@Override
public void selectByVisibleText(String text) {
assertSelectIsEnabled();

assertSelectIsVisible();
// try to find the option via XPATH ...
List<WebElement> 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;
}
}

boolean matched = !options.isEmpty();
if (!matched && text.contains(" ")) {
String subStringWithoutSpace = getLongestSubstringWithoutSpace(text);
List<WebElement> candidates;
if ("".equals(subStringWithoutSpace)) {
if (!matched) {
String searchText = text.contains(" ") ? getLongestSubstringWithoutSpace(text) : text;
List<WebElement> 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();
Expand Down