@@ -122,55 +122,7 @@ public WebElement getFirstSelectedOption() {
122122 */
123123 @ Override
124124 public void selectByVisibleText (String text ) {
125- assertSelectIsEnabled ();
126- assertSelectIsVisible ();
127-
128- // try to find the option via XPATH ...
129- List <WebElement > options =
130- element .findElements (
131- By .xpath (".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
132-
133- for (WebElement option : options ) {
134- if (!hasCssPropertyAndVisible (option ))
135- throw new NoSuchElementException ("Invisible option with text: " + text );
136- setSelected (option , true );
137- if (!isMultiple ()) {
138- return ;
139- }
140- }
141-
142- boolean matched = !options .isEmpty ();
143- if (!matched && text .contains (" " )) {
144- String subStringWithoutSpace = getLongestSubstringWithoutSpace (text );
145- List <WebElement > candidates ;
146- if ("" .equals (subStringWithoutSpace )) {
147- // hmm, text is either empty or contains only spaces - get all options ...
148- candidates = element .findElements (By .tagName ("option" ));
149- } else {
150- // get candidates via XPATH ...
151- candidates =
152- element .findElements (
153- By .xpath (".//option[contains(., " + Quotes .escape (subStringWithoutSpace ) + ")]" ));
154- }
155-
156- String trimmed = text .trim ();
157-
158- for (WebElement option : candidates ) {
159- if (trimmed .equals (option .getText ().trim ())) {
160- if (!hasCssPropertyAndVisible (option ))
161- throw new NoSuchElementException ("Invisible option with text: " + text );
162- setSelected (option , true );
163- if (!isMultiple ()) {
164- return ;
165- }
166- matched = true ;
167- }
168- }
169- }
170-
171- if (!matched ) {
172- throw new NoSuchElementException ("Cannot locate option with text: " + text );
173- }
125+ selectByVisibleText (text , false );
174126 }
175127
176128 /**
@@ -193,53 +145,7 @@ public void selectByVisibleText(String text) {
193145 */
194146 @ Override
195147 public void selectByContainsVisibleText (String text ) {
196- assertSelectIsEnabled ();
197- assertSelectIsVisible ();
198-
199- // try to find the option via XPATH ...
200- List <WebElement > options =
201- element .findElements (
202- By .xpath (".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
203-
204- for (WebElement option : options ) {
205- if (!hasCssPropertyAndVisible (option ))
206- throw new NoSuchElementException ("Invisible option with text: " + text );
207- setSelected (option , true );
208- if (!isMultiple ()) {
209- return ;
210- }
211- }
212-
213- boolean matched = !options .isEmpty ();
214- if (!matched ) {
215- String searchText = text .contains (" " ) ? getLongestSubstringWithoutSpace (text ) : text ;
216-
217- List <WebElement > candidates ;
218- if (searchText .isEmpty ()) {
219- candidates = element .findElements (By .tagName ("option" ));
220- } else {
221- candidates =
222- element .findElements (
223- By .xpath (".//option[contains(., " + Quotes .escape (searchText ) + ")]" ));
224- }
225-
226- String trimmed = text .trim ();
227- for (WebElement option : candidates ) {
228- if (option .getText ().contains (trimmed )) {
229- if (!hasCssPropertyAndVisible (option ))
230- throw new NoSuchElementException ("Invisible option with text: " + text );
231- setSelected (option , true );
232- if (!isMultiple ()) {
233- return ;
234- }
235- matched = true ;
236- }
237- }
238- }
239-
240- if (!matched ) {
241- throw new NoSuchElementException ("Cannot locate option with text: " + text );
242- }
148+ selectByVisibleText (text , true );
243149 }
244150
245151 private String getLongestSubstringWithoutSpace (String s ) {
@@ -425,6 +331,73 @@ private void setSelected(WebElement option, boolean select) {
425331 }
426332 }
427333
334+ /**
335+ * @param text The visible text to match against. It can be a partial match of the option if
336+ * isPartialMatch = true
337+ * @param isPartialMatch If true a partial match on the Options list will be performed, otherwise
338+ * exact match
339+ * @throws NoSuchElementException If no matching option elements are found or matching options are
340+ * hidden
341+ */
342+ private void selectByVisibleText (String text , boolean isPartialMatch ) {
343+ assertSelectIsEnabled ();
344+ assertSelectIsVisible ();
345+
346+ // try to find the option via XPATH ...
347+ List <WebElement > options =
348+ element .findElements (
349+ By .xpath (".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
350+
351+ for (WebElement option : options ) {
352+ if (!hasCssPropertyAndVisible (option ))
353+ throw new NoSuchElementException ("Invisible option with text: " + text );
354+
355+ setSelected (option , true );
356+
357+ if (!isMultiple ()) {
358+ return ;
359+ }
360+ }
361+
362+ boolean matched = !options .isEmpty ();
363+ if (!matched ) {
364+ String searchText = text .contains (" " ) ? getLongestSubstringWithoutSpace (text ) : text ;
365+
366+ List <WebElement > candidates ;
367+ if (searchText .isEmpty ()) {
368+ candidates = element .findElements (By .tagName ("option" ));
369+ } else {
370+ candidates =
371+ element .findElements (
372+ By .xpath (".//option[contains(., " + Quotes .escape (searchText ) + ")]" ));
373+ }
374+
375+ String trimmed = text .trim ();
376+ for (WebElement option : candidates ) {
377+ boolean isMatchedOptionFound =
378+ isPartialMatch
379+ ? option .getText ().contains (trimmed )
380+ : option .getText ().trim ().equals (trimmed );
381+
382+ if (isMatchedOptionFound ) {
383+ if (!hasCssPropertyAndVisible (option ))
384+ throw new NoSuchElementException ("Invisible option with text: " + text );
385+
386+ setSelected (option , true );
387+
388+ if (!isMultiple ()) {
389+ return ;
390+ }
391+ matched = true ;
392+ }
393+ }
394+ }
395+
396+ if (!matched ) {
397+ throw new NoSuchElementException ("Cannot locate option with text: " + text );
398+ }
399+ }
400+
428401 private void assertOptionIsEnabled (WebElement option , boolean select ) {
429402 if (select && !option .isEnabled ()) {
430403 throw new UnsupportedOperationException ("You may not select a disabled option" );
0 commit comments