0

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>
  • 1
    What if you move the selection to after opening the drop-down? – jonrsharpe Aug 1 at 9:52
  • still receiving the error, i do not get past the IList<IWebElement> selectdelegation step – user40734 Aug 1 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 Aug 1 at 10:05
  • honestly i don't know what to put there, [1] there it should go a integer value, not a id – user40734 Aug 1 at 10:17
  • Right, but what's the index of the first thing in a list? – jonrsharpe Aug 1 at 10:19
1

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

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, privacy policy and cookie policy

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