Unable to select <li> element from a <ul> list



  • 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 
    Thread.Sleep(500);
    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…
        </li>
    </ul>
    


  • 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 ); 
    


Suggested Topics