Gets a mechanism to find elements by their ID. The ID to find. A object the driver can use to find the elements. If is .
(string idToFind)
| 145 | /// <returns>A <see cref="By"/> object the driver can use to find the elements.</returns> |
| 146 | /// <exception cref="ArgumentNullException">If <paramref name="idToFind"/> is <see langword="null"/>.</exception> |
| 147 | public static By Id(string idToFind) |
| 148 | { |
| 149 | if (idToFind == null) |
| 150 | { |
| 151 | throw new ArgumentNullException(nameof(idToFind), "Cannot find elements with a null id attribute."); |
| 152 | } |
| 153 | |
| 154 | string selector = EscapeCssSelector(idToFind); |
| 155 | By by = new By(CssSelectorMechanism, "#" + selector); |
| 156 | by.Description = "By.Id: " + idToFind; |
| 157 | if (string.IsNullOrEmpty(selector)) |
| 158 | { |
| 159 | // Finding multiple elements with an empty ID will return |
| 160 | // an empty list. However, finding by a CSS selector of '#' |
| 161 | // throws an exception, even in the multiple elements case, |
| 162 | // which means we need to short-circuit that behavior. |
| 163 | by.FindElementsMethod = (ISearchContext context) => new List<IWebElement>().AsReadOnly(); |
| 164 | } |
| 165 | |
| 166 | return by; |
| 167 | } |
| 168 | |
| 169 | /// <summary> |
| 170 | /// Gets a mechanism to find elements by their link text. |