C# 如何获取a<;ul>;自动列出并选择一个随机选项
我无法选择标记列表中的项目,C# 如何获取a<;ul>;自动列出并选择一个随机选项,c#,list,selenium,C#,List,Selenium,我无法选择标记列表中的项目,中列出的所有选项。基本上,我需要知道项目的数量,并选择一个(任何)。当前代码无法选择do to,我无法找到正确的索引,我必须手动搜索正确的索引 我尝试对列表中的每个元素使用 如何搜索索引和显示的选项: foreach (var a in nameoflist) { Console.WriteLine("ddm" + a.Text); } 如何选择列表中的项目: List<IWebE
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();
列表下拉菜单=driver.FindElements(By.ClassName(“下拉菜单”);
IList selectdomain=dropDownMenu[0]。FindElements(按.TagName(“li”));
选择域[0]。单击();
外部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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && 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() && !moveInProgress && (matches[0] && matches[0].label == '')"><span translate="" class="ng-scope">No result found</span></div></li>
</ul>
-
-
-
-
-
-
-
-
-
-
- 没有找到结果
李的HTML
<li class="uib-typeahead-match ng-scope" ng-show="isOpen() && !moveInProgress && (matches[0] && 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">
我总是收到“索引超出范围”错误,我必须手动搜索有效的索引。如何自动选择任何选项?我从未使用过selenium,但您可以尝试以下操作:
// Get ul.dropdown-menu > li
List<WebElement> dropdownMenuItems = driver.findElements(By.xpath("//ul[@class='dropdown-menu']/li"));
// Get Quantity
int quantity = dropdownMenuItems.Count;
// Create instance of random
Random rnd = new Random();
// Get a random index between 0 and the max being list length
int randomIndex = rnd.Next(quantity);
// Get the list item from the random index
var randomListItem = dropdownMenuItems[randomIndex];
// Click the element
randomListItem.Click();
// Optiona: Loop through items
foreach (var listItem in dropdownMenuItems)
{
Console.WriteLine("ddm" listItem a.Text);
}
//获取ul.dropdown-menu>li
List-dropdownMenuItems=driver.findElements(By.xpath(“//ul[@class='dropdown-menu']/li”);
//获取数量
int quantity=dropdownMenuItems.Count;
//创建实例
IList<IWebElement> selectdomain = dropDownMenu[0].FindElements(By.TagName("li"));
var count = selectdomain.Count();
if (count > 0){
selectdomain[0].Click();
}