public void SelectByText(string text) { if (text == null) { throw new ArgumentNullException("text", "text must not be null"); } IList <IWebElement> list = this.element.FindElements(By.XPath(".//option[normalize-space(.) = " + SelectElement.EscapeQuotes(text) + "]")); bool flag = false; foreach (IWebElement current in list) { SelectElement.SetSelected(current); if (!this.IsMultiple) { return; } flag = true; } if (list.Count == 0 && text.Contains(" ")) { string longestSubstringWithoutSpace = SelectElement.GetLongestSubstringWithoutSpace(text); IList <IWebElement> list2; if (string.IsNullOrEmpty(longestSubstringWithoutSpace)) { list2 = this.element.FindElements(By.TagName("option")); } else { list2 = this.element.FindElements(By.XPath(".//option[contains(., " + SelectElement.EscapeQuotes(longestSubstringWithoutSpace) + ")]")); } foreach (IWebElement current2 in list2) { if (text == current2.Text) { SelectElement.SetSelected(current2); if (!this.IsMultiple) { return; } flag = true; } } } if (!flag) { throw new NoSuchElementException("Cannot locate element with text: " + text); } }