@@ -116,7 +116,7 @@ public void selectByVisibleText(String text) {
116
116
117
117
boolean matched = false ;
118
118
for (WebElement option : options ) {
119
- setSelected (option );
119
+ setSelected (option , true );
120
120
if (!isMultiple ()) {
121
121
return ;
122
122
}
@@ -137,7 +137,7 @@ public void selectByVisibleText(String text) {
137
137
}
138
138
for (WebElement option : candidates ) {
139
139
if (text .equals (option .getText ())) {
140
- setSelected (option );
140
+ setSelected (option , true );
141
141
if (!isMultiple ()) {
142
142
return ;
143
143
}
@@ -173,19 +173,13 @@ private String getLongestSubstringWithoutSpace(String s) {
173
173
public void selectByIndex (int index ) {
174
174
String match = String .valueOf (index );
175
175
176
- boolean matched = false ;
177
176
for (WebElement option : getOptions ()) {
178
177
if (match .equals (option .getAttribute ("index" ))) {
179
- setSelected (option );
180
- if (!isMultiple ()) {
181
- return ;
182
- }
183
- matched = true ;
178
+ setSelected (option , true );
179
+ return ;
184
180
}
185
181
}
186
- if (!matched ) {
187
- throw new NoSuchElementException ("Cannot locate option with index: " + index );
188
- }
182
+ throw new NoSuchElementException ("Cannot locate option with index: " + index );
189
183
}
190
184
191
185
/**
@@ -203,7 +197,7 @@ public void selectByValue(String value) {
203
197
204
198
boolean matched = false ;
205
199
for (WebElement option : options ) {
206
- setSelected (option );
200
+ setSelected (option , true );
207
201
if (!isMultiple ()) {
208
202
return ;
209
203
}
@@ -227,9 +221,7 @@ public void deselectAll() {
227
221
}
228
222
229
223
for (WebElement option : getOptions ()) {
230
- if (option .isSelected ()) {
231
- option .click ();
232
- }
224
+ setSelected (option , false );
233
225
}
234
226
}
235
227
@@ -241,15 +233,23 @@ public void deselectAll() {
241
233
*
242
234
* @param value The value to match against
243
235
* @throws NoSuchElementException If no matching option elements are found
236
+ * @throws UnsupportedOperationException If the SELECT does not support multiple selections
244
237
*/
245
238
public void deselectByValue (String value ) {
239
+ if (!isMultiple ()) {
240
+ throw new UnsupportedOperationException (
241
+ "You may only deselect options of a multi-select" );
242
+ }
243
+
246
244
List <WebElement > options = element .findElements (By .xpath (
247
245
".//option[@value = " + Quotes .escape (value ) + "]" ));
248
-
246
+ boolean matched = false ;
249
247
for (WebElement option : options ) {
250
- if (option .isSelected ()) {
251
- option .click ();
252
- }
248
+ setSelected (option , false );
249
+ matched = true ;
250
+ }
251
+ if (!matched ) {
252
+ throw new NoSuchElementException ("Cannot locate option with value: " + value );
253
253
}
254
254
}
255
255
@@ -259,15 +259,23 @@ public void deselectByValue(String value) {
259
259
*
260
260
* @param index The option at this index will be deselected
261
261
* @throws NoSuchElementException If no matching option elements are found
262
+ * @throws UnsupportedOperationException If the SELECT does not support multiple selections
262
263
*/
263
264
public void deselectByIndex (int index ) {
265
+ if (!isMultiple ()) {
266
+ throw new UnsupportedOperationException (
267
+ "You may only deselect options of a multi-select" );
268
+ }
269
+
264
270
String match = String .valueOf (index );
265
-
271
+
266
272
for (WebElement option : getOptions ()) {
267
- if (match .equals (option .getAttribute ("index" )) && option .isSelected ()) {
268
- option .click ();
273
+ if (match .equals (option .getAttribute ("index" ))) {
274
+ setSelected (option , false );
275
+ return ;
269
276
}
270
277
}
278
+ throw new NoSuchElementException ("Cannot locate option with index: " + index );
271
279
}
272
280
273
281
/**
@@ -278,20 +286,40 @@ public void deselectByIndex(int index) {
278
286
*
279
287
* @param text The visible text to match against
280
288
* @throws NoSuchElementException If no matching option elements are found
289
+ * @throws UnsupportedOperationException If the SELECT does not support multiple selections
281
290
*/
282
291
public void deselectByVisibleText (String text ) {
292
+ if (!isMultiple ()) {
293
+ throw new UnsupportedOperationException (
294
+ "You may only deselect options of a multi-select" );
295
+ }
296
+
283
297
List <WebElement > options = element .findElements (By .xpath (
284
298
".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
285
299
300
+ boolean matched = false ;
286
301
for (WebElement option : options ) {
287
- if (option .isSelected ()) {
288
- option .click ();
289
- }
302
+ setSelected (option , false );
303
+ matched = true ;
304
+ }
305
+
306
+ if (!matched ) {
307
+ throw new NoSuchElementException ("Cannot locate element with text: " + text );
290
308
}
291
309
}
292
310
293
- private void setSelected (WebElement option ) {
294
- if (!option .isSelected ()) {
311
+ /**
312
+ * Select or deselect specified option
313
+ *
314
+ * @param option
315
+ * The option which state needs to be changed
316
+ * @param select
317
+ * Indicates whether the option needs to be selected (true) or
318
+ * deselected (false)
319
+ */
320
+ private void setSelected (WebElement option , boolean select ) {
321
+ boolean isSelected =option .isSelected ();
322
+ if ((!isSelected && select ) || (isSelected && !select )) {
295
323
option .click ();
296
324
}
297
325
}
0 commit comments