Skip to content

[Java] Add visibility check to selectByVisibleText in Select class #15912

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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions java/src/org/openqa/selenium/support/ui/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,30 @@ 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) + "]"));

boolean selectedAnyVisible = false;

for (WebElement option : options) {
if (!hasCssPropertyAndVisible(option)) {
throw new NoSuchElementException("Invisible option with text: " + text);
}
setSelected(option, 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);
Expand Down