我在WebDriver中阅读了一些关于随机选择的帖子,但无法让它对我有用。我需要从下拉菜单中选择随机选项。只有在单击下拉列表并且页面中显示选项时,才会在控制台中显示菜单的html代码(我发布的那个)。我给你的是我在C#中使用的代码。如果您需要任何其他信息,请随时询问。
这是html
<input class="form-control ui-select-search ng-pristine ng-valid ng-touched" autocomplete="off" tabindex="-1" aria-expanded="true" aria-label="Select box" aria-owns="ui-select-choices-9" aria-activedescendant="ui-select-choices-row-9-2" placeholder="Select material" ng-model="$select.search" ng-show="$select.searchEnabled && $select.open" type="text">
<ul class="ui-select-choices ui-select-choices-content dropdown-menu" role="listbox" ng-show="$select.items.length > 0" repeat="material in materialsForTransfer | array | propsFilter: {material_name: $select.search}">
<li id="ui-select-choices-9" class="ui-select-choices-group">
<div class="divider ng-hide" ng-show="$select.isGrouped && $index > 0"></div>
<div class="ui-select-choices-group-label dropdown-header ng-hide" ng-show="$select.isGrouped" ng-bind="$group.name"></div>
<div id="ui-select-choices-row-9-0" class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="material in $select.items" ng-if="$select.open" ng-mouseenter="$select.setActiveItem(material)" ng-click="$select.select(material,false,$event)">
<a class="ui-select-choices-row-inner" href="javascript:void(0)" uis-transclude-append="">
<div ng-bind-html="material.material_name | highlight: $select.search">LG Monitor</div>
</a>
</div>
<div id="ui-select-choices-row-9-1" class="ui-select-choices-row active" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="material in $select.items" ng-if="$select.open" ng-mouseenter="$select.setActiveItem(material)" ng-click="$select.select(material,false,$event)">
<a class="ui-select-choices-row-inner" href="javascript:void(0)" uis-transclude-append="">
<div ng-bind-html="material.material_name | highlight: $select.search">DELL Monitor</div>
</a>
</div>
<div id="ui-select-choices-row-9-2" class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="material in $select.items" ng-if="$select.open" ng-mouseenter="$select.setActiveItem(material)" ng-click="$select.select(material,false,$event)">
<a class="ui-select-choices-row-inner" href="javascript:void(0)" uis-transclude-append="">
<div ng-bind-html="material.material_name | highlight: $select.search">CPU i5</div>
</a>
</div>
<div id="ui-select-choices-row-9-3" class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="material in $select.items" ng-if="$select.open" ng-mouseenter="$select.setActiveItem(material)" ng-click="$select.select(material,false,$event)">
<a class="ui-select-choices-row-inner" href="javascript:void(0)" uis-transclude-append="">
<div ng-bind-html="material.material_name | highlight: $select.search">HDD 1TB</div>
</a>
</div>
</li>
</ul>
我在C#中的代码
public class OrderTransferPage
{
[FindsBy(How = How.CssSelector, Using = "span[ng- click='$select.activate()']")][CacheLookup]
private IWebElement ClickOnNameOfProductMaterial { get; set; }
[FindsBy(How = How.CssSelector, Using = "div[ng-bind-html='material.material_name']")][CacheLookup]
private IList<IWebElement> NameOfProductMaterial { get; set; }
}
public void OpenOT()
{
ClickOnNameOfProductMaterial.ClickOnIt("Select Material", 15); //When I click on this element the drop down is generated.
Data.RandomElement(NameOfProductMaterial).ClickOnIt("Selecting random element");
}
public static IWebElement RandomElement (this IList<IWebElement> element)
{
Random rnd = new Random();
int randomValue = rnd.Next(element.Count);
IWebElement OurElement = element[randomValue];
return OurElement;
}
答案 0 :(得分:3)
你说你正在使用下拉列表。我假设下拉列表是一个实际的HTML SELECT
元素。如果这不正确,那么此代码将不起作用。让我知道,我可以适应它。
我会利用SelectElement
课程。它使SELECT
的使用更容易。我会做类似下面的事情。
注意:我对您的功能进行了一些更改。此函数不返回任何内容,它接收SelectElement
,生成随机数(与您的一样),然后选择与该随机索引对应的OPTION
。
static void SelectRandomElement(SelectElement select)
{
Random rnd = new Random();
int index = rnd.Next(select.Options.Count);
select.SelectByIndex(index);
}
要使用此功能,您可以执行类似
的操作RandomElement(new SelectElement(selectElement));
答案 1 :(得分:0)
我设法让它工作,我发布了解决方案,所以任何人都可以在需要时使用它。下拉列表中的所有元素都具有相同的“ng-bind-html”,并且只有不同的链接文本。因此,使用向下代码,我列出了下拉列表中的所有元素。
`[FindsBy(How = How.CssSelector, Using = "div[ng-bind-html='material.material_number | highlight: $select.search']")][CacheLookup]
private IList<IWebElement> NameOfProductMaterial { get; set; }`
正如您在乞讨时从HTML中看到的那样,我选择了最嵌套的元素。
`Element_Extensions.RandomElement(NameOfProductMaterial).ClickOnIt("Select Material");`
访问进行随机选择的方法。
`public static IWebElement RandomElement(this IList<IWebElement> element)
{
Random rnd = new Random();
int randomValue = rnd.Next(1, (element.Count)-1);
IWebElement OurElement = element[randomValue];
Console.WriteLine(OurElement.Text + " is selected material");
return OurElement;
}`
乞讨的问题是FindsBy
行中的元素选择错误。
P.S。我希望这些信息对任何人都有帮助。并JeffC感谢您的时间和帮助。你让我思考并发现了错误。