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); } }
public void SelectByIndex(int index) { string b = index.ToString(CultureInfo.InvariantCulture); bool flag = false; foreach (IWebElement current in this.Options) { if (current.GetAttribute("index") == b) { SelectElement.SetSelected(current); if (!this.IsMultiple) { return; } flag = true; } } if (!flag) { throw new NoSuchElementException("Cannot locate option with index: " + index); } }
public void SelectByValue(string value) { StringBuilder stringBuilder = new StringBuilder(".//option[@value = "); stringBuilder.Append(SelectElement.EscapeQuotes(value)); stringBuilder.Append("]"); IList <IWebElement> list = this.element.FindElements(By.XPath(stringBuilder.ToString())); bool flag = false; foreach (IWebElement current in list) { SelectElement.SetSelected(current); if (!this.IsMultiple) { return; } flag = true; } if (!flag) { throw new NoSuchElementException("Cannot locate option with value: " + value); } }