How do I select a "li" element from a "ul" tag list?

This is my current code

vtenants.VT_USDelselectfld.Click(); //Clicks field to display the dropdown 
IList<IWebElement> dropDownMenu = driver.FindElements(By.CssSelector("#typeahead-1358-1464")); //Populates all drop down menues
IList<IWebElement> selectdelegation = dropDownMenu[1].FindElements(By.TagName("li")); //Here is where i receive the error
selectdelegation[1].Click(); //Here i "should" select the dropdown Item

I receive the error " Index was out of range. Must be non-negative and less than the size of the collection."

HTML Example

<ul class="dropdown-menu ng-isolate-scope" ng-show="isOpen() 
    <li class="uib-typeahead-match ng-scope" ng-repeat="match in matches 
    track by $index" ng-class="{active: isActive($index) }" etc…
  • 1
    What if you move the selection to after opening the drop-down?
    – jonrsharpe
    Commented Aug 1, 2019 at 9:52
  • still receiving the error, i do not get past the IList<IWebElement> selectdelegation step
    – user40734
    Commented Aug 1, 2019 at 10:04
  • [1] looks suspicious for a selector based on an ID, because there should only ever be one element with a given ID.
    – jonrsharpe
    Commented Aug 1, 2019 at 10:05
  • honestly i don't know what to put there, [1] there it should go a integer value, not a id
    – user40734
    Commented Aug 1, 2019 at 10:17
  • Right, but what's the index of the first thing in a list?
    – jonrsharpe
    Commented Aug 1, 2019 at 10:19

2 Answers 2


Your locator for the list is wrong, there is not typeahead-1358-1464 on the HTML.

You can proceed as follows:

// You may need to update the locator for uniqueness
listLocator = By.CssSelector("ul.dropdown-menu.ng-isolate-scope");
itemLocator = By.TagName("li");

items = driver.FindElement(listLocator )).FindElements(itemLocator ); 

try this: IList<IWebElement> selectdelegation = driver.FindElements(By.CssSelector("#typeahead-1358-1464 li")); //maybe not the best selector - can be improved.

The main idea is to use only one list. Also you can stream and filter the "selectdelegation" and return only a part of the elements as a IList of strings for assertions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.