1
1
/*
2
+ Copyright 2015 Software Freedom Conservancy
2
3
Copyright 2007-2009 Selenium committers
3
4
4
5
Licensed under the Apache License, Version 2.0 (the "License");
@@ -111,7 +112,7 @@ public WebElement getFirstSelectedOption() {
111
112
public void selectByVisibleText (String text ) {
112
113
// try to find the option via XPATH ...
113
114
List <WebElement > options =
114
- element .findElements (By .xpath (".//option[normalize-space(.) = " + escapeQuotes (text ) + "]" ));
115
+ element .findElements (By .xpath (".//option[normalize-space(.) = " + Quotes . escape (text ) + "]" ));
115
116
116
117
boolean matched = false ;
117
118
for (WebElement option : options ) {
@@ -132,7 +133,7 @@ public void selectByVisibleText(String text) {
132
133
// get candidates via XPATH ...
133
134
candidates =
134
135
element .findElements (By .xpath (".//option[contains(., " +
135
- escapeQuotes (subStringWithoutSpace ) + ")]" ));
136
+ Quotes . escape (subStringWithoutSpace ) + ")]" ));
136
137
}
137
138
for (WebElement option : candidates ) {
138
139
if (text .equals (option .getText ())) {
@@ -163,7 +164,7 @@ private String getLongestSubstringWithoutSpace(String s) {
163
164
}
164
165
165
166
/**
166
- * Select the option at the given index. This is done by examing the "index" attribute of an
167
+ * Select the option at the given index. This is done by examining the "index" attribute of an
167
168
* element, and not merely by counting.
168
169
*
169
170
* @param index The option at this index will be selected
@@ -197,10 +198,8 @@ public void selectByIndex(int index) {
197
198
* @throws NoSuchElementException If no matching option elements are found
198
199
*/
199
200
public void selectByValue (String value ) {
200
- StringBuilder builder = new StringBuilder (".//option[@value = " );
201
- builder .append (escapeQuotes (value ));
202
- builder .append ("]" );
203
- List <WebElement > options = element .findElements (By .xpath (builder .toString ()));
201
+ List <WebElement > options = element .findElements (By .xpath (
202
+ ".//option[@value = " + Quotes .escape (value ) + "]" ));
204
203
205
204
boolean matched = false ;
206
205
for (WebElement option : options ) {
@@ -244,10 +243,9 @@ public void deselectAll() {
244
243
* @throws NoSuchElementException If no matching option elements are found
245
244
*/
246
245
public void deselectByValue (String value ) {
247
- StringBuilder builder = new StringBuilder (".//option[@value = " );
248
- builder .append (escapeQuotes (value ));
249
- builder .append ("]" );
250
- List <WebElement > options = element .findElements (By .xpath (builder .toString ()));
246
+ List <WebElement > options = element .findElements (By .xpath (
247
+ ".//option[@value = " + Quotes .escape (value ) + "]" ));
248
+
251
249
for (WebElement option : options ) {
252
250
if (option .isSelected ()) {
253
251
option .click ();
@@ -256,7 +254,7 @@ public void deselectByValue(String value) {
256
254
}
257
255
258
256
/**
259
- * Deselect the option at the given index. This is done by examing the "index" attribute of an
257
+ * Deselect the option at the given index. This is done by examining the "index" attribute of an
260
258
* element, and not merely by counting.
261
259
*
262
260
* @param index The option at this index will be deselected
@@ -282,44 +280,16 @@ public void deselectByIndex(int index) {
282
280
* @throws NoSuchElementException If no matching option elements are found
283
281
*/
284
282
public void deselectByVisibleText (String text ) {
285
- StringBuilder builder = new StringBuilder (".//option[normalize-space(.) = " );
286
- builder .append (escapeQuotes (text ));
287
- builder .append ("]" );
288
- List <WebElement > options = element .findElements (By .xpath (builder .toString ()));
283
+ List <WebElement > options = element .findElements (By .xpath (
284
+ ".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
285
+
289
286
for (WebElement option : options ) {
290
287
if (option .isSelected ()) {
291
288
option .click ();
292
289
}
293
290
}
294
291
}
295
292
296
- protected String escapeQuotes (String toEscape ) {
297
- // Convert strings with both quotes and ticks into: foo'"bar -> concat("foo'", '"', "bar")
298
- if (toEscape .indexOf ("\" " ) > -1 && toEscape .indexOf ("'" ) > -1 ) {
299
- boolean quoteIsLast = false ;
300
- if (toEscape .lastIndexOf ("\" " ) == toEscape .length () - 1 ) {
301
- quoteIsLast = true ;
302
- }
303
- String [] substrings = toEscape .split ("\" " );
304
-
305
- StringBuilder quoted = new StringBuilder ("concat(" );
306
- for (int i = 0 ; i < substrings .length ; i ++) {
307
- quoted .append ("\" " ).append (substrings [i ]).append ("\" " );
308
- quoted
309
- .append (((i == substrings .length - 1 ) ? (quoteIsLast ? ", '\" ')" : ")" ) : ", '\" ', " ));
310
- }
311
- return quoted .toString ();
312
- }
313
-
314
- // Escape string with just a quote into being single quoted: f"oo -> 'f"oo'
315
- if (toEscape .indexOf ("\" " ) > -1 ) {
316
- return String .format ("'%s'" , toEscape );
317
- }
318
-
319
- // Otherwise return the quoted string
320
- return String .format ("\" %s\" " , toEscape );
321
- }
322
-
323
293
private void setSelected (WebElement option ) {
324
294
if (!option .isSelected ()) {
325
295
option .click ();
0 commit comments