Select an option by the value. The value of the option to be selected. When given "foo" this method will select an option like: <option value="foo">Bar</option> Thrown when no element with the specified value is found.
(string value)
| 201 | /// </remarks> |
| 202 | /// <exception cref="NoSuchElementException">Thrown when no element with the specified value is found.</exception> |
| 203 | public void SelectByValue(string value) |
| 204 | { |
| 205 | StringBuilder builder = new StringBuilder(".//option[@value = "); |
| 206 | builder.Append(EscapeQuotes(value)); |
| 207 | builder.Append("]"); |
| 208 | IList<IWebElement> options = this.WrappedElement.FindElements(By.XPath(builder.ToString())); |
| 209 | |
| 210 | bool matched = false; |
| 211 | foreach (IWebElement option in options) |
| 212 | { |
| 213 | SetSelected(option, true); |
| 214 | if (!this.IsMultiple) |
| 215 | { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | matched = true; |
| 220 | } |
| 221 | |
| 222 | if (!matched) |
| 223 | { |
| 224 | throw new NoSuchElementException("Cannot locate option with value: " + value); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /// <summary> |
| 229 | /// Select the option by the index, as determined by the "index" attribute of the element. |