如何获取<ul>列表的索引并自动选择随机选项

时间:2019-09-04 08:38:51

标签: c# list selenium

我无法选择标签列表中的项目,

  • 中列出了所有选项。基本上,我需要知道项目的数量并选择一个(任意)。当前代码无法选择要执行的操作,因为我无法找到正确的索引,我必须手动搜索正确的索引

    我尝试对列表中的每个元素都使用

    我如何搜索显示的索引和选项:

    foreach (var a in nameoflist)
                {
                    Console.WriteLine("ddm" + a.Text);
                }
    

    我如何选择列表中的项目:

    List<IWebElement> dropDownMenu = driver.FindElements(By.ClassName("dropdown-menu"));
    IList<IWebElement> selectdomain = dropDownMenu[0].FindElements(By.TagName("li"));
    selectdomain[0].Click();
    

    外部HTML:

    <ul class="dropdown-menu ng-isolate-scope" ng-class="{'closed-typeahed' : !isOpen()}" ng-style="{top: position().top+'px', left: position().left+'px'}" role="listbox" aria-hidden="false" uib-typeahead-popup="" id="typeahead-1511-7724" matches="matches" active="activeIdx" select="select(activeIdx, evt)" move-in-progress="moveInProgress" query="query" position="position" assign-is-open="assignIsOpen(isOpen)" debounce="debounceUpdate" popup-template-url="typeahead-custom-popup.html" style="top: 34px; left: 0px;">
                <!-- ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-0">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="csv.user03@coreviewlab.com">csv.<strong>user</strong>03@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-1">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="csv.user@coreviewlab.com">csv.<strong>user</strong>@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-2">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="synch.user7@coreviewlab.com">synch.<strong>user</strong>7@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope active" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-3">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="hybrid.usermodified@coreviewlab.com">hybrid.<strong>user</strong>modified@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-4">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="csv.user02@coreviewlab.com">csv.<strong>user</strong>02@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-5">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="csve.user04@coreviewlab.com">csve.<strong>user</strong>04@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-6">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="csve.user05@coreviewlab.com">csve.<strong>user</strong>05@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-7">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="testuser.01@coreviewlab.com">test<strong>user</strong>.01@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-8">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="carmine.syncuser@coreviewlab.com">carmine.sync<strong>user</strong>@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index --><li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-9">
                    <a href="" tabindex="-1" ng-bind-html="match.label | uibTypeaheadHighlight:query" ng-attr-title="{{match.label}}" class="ng-binding ng-scope" title="newhybrid.user@coreviewlab.com">newhybrid.<strong>user</strong>@coreviewlab.com</a>
                </li><!-- end ngRepeat: match in matches track by $index -->
                <li><div class="typeahead-no-results small ng-hide" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label == '')"><span translate="" class="ng-scope">No result found</span></div></li>
            </ul>
    

    li的HTML

    <li class="uib-typeahead-match ng-scope" ng-show="isOpen() &amp;&amp; !moveInProgress &amp;&amp; (matches[0] &amp;&amp; matches[0].label != '')" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="typeahead-1511-7724-option-0">
    

    我总是收到“索引超出范围”错误。并且,我必须手动搜索有效的索引。我该如何自动选择任何选项?

  • 2 个答案:

    答案 0 :(得分:1)

    尝试将计数验证放在首位

    IList<IWebElement> selectdomain = dropDownMenu[0].FindElements(By.TagName("li"));
    
    var count = selectdomain.Count();
    if (count > 0){
        selectdomain[0].Click();
    }
    

    答案 1 :(得分:0)

    我从未使用过硒,但是您可以尝试以下方法:

    curl_setopt($ch, CURLOPT_ENCODING, '');