简体   繁体   English

如何使用量角器在dropdowlist中随机获取元素?

[英]How can I get an element randomly in a dropdowlist using protractor?

I've this dropdownlist and I'm trying to get a value randomly and click on it. 我有这个下拉列表,我正在尝试随机获取一个值并单击它。 How can I do it? 我该怎么做? I can't use the class because there is other element with the same class. 我不能使用该类,因为有其他元素具有相同的类。 I don't have a clue. 我没有线索。

<dropdownlist _ngcontent-lnd-30="">
<select class="form-control ng-pristine ng-valid ng-touched">
        <!--template bindings={}-->
<option value="null">Selecione um tipo de norma...</option>
<option value="5980dfc1-ed08-4e5f-bdd7-144beb2fafe3">Enunciado Orientativo</option>
<option value="e721782a-11ba-4828-ac3a-934f60652760">Instrução Normativa</option>
<option value="a4469d22-1188-467d-a78a-e385a2cc8eb9">Lei</option>
<option value="9d8ea2fd-efe9-410a-8062-f5607c56332d">Portaria</option>
<option value="8407a52d-a760-48a2-b780-ab93f5904565">Provimento</option>
<option value="8b20cc7f-6be1-43a5-a0b7-ac2fe695b14c">Resolução</option>
<option value="8fe058a8-ece3-4ef5-8f74-17255a90066f">Súmula</option>
 </select></dropdownlist>

Count the number of options (it's asynchronous, so you will be chaining some promises), then get a random number between 1 and the total count, then get that specific element and click on it. 计算选项的数量(它是异步的,所以你将链接一些promises),然后获得1和总计数之间的随机数,然后获取该特定元素并单击它。

var allOptions = element(by.tagName('dropdownlist')).all(by.tagName('option'));

allOptions.count().then(function(numberOfItems) {
    return Math.floor(Math.random() * numberOfItems) + 1;
}).then(function(randomNumber) {
    allOptions.get(randomNumber).click();
});

You can select a random index of the options array like so. 您可以选择选项数组的随机索引。

// get your options first
// var options = angular.element(...) or getElementByID(...) Whatever your flavor.
var selectRandomOption = options[Math.floor(Math.random() * options.length)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从带有量角器的元素中获取子项数量? - How can I get the number of children from an element with protractor? 我如何从一个元素中获得不同的孩子 - Protractor - How can i get different childs from one element - Protractor 如何使用量角器获取当前网址? - how can I get the current url using protractor? 如何使用量角器单击此元素按钮? - How can this element button be clicked using protractor? 如何使用javascript获取dropdowlist的选定值 - How to get the selected value of a dropdowlist with javascript 如何防止使用JavaScript在dropdowlist中选择项目? - how to prevent select item in dropdowlist using javascript? 如何使用css使用量角器元素从组件中获取元素 - how to get element from an component using protractor element by css 我怎样才能用黄瓜覆盖量角器 - How can I get coverage of protractor with cucumber 量角器,使用 isDisplayed() 我得到 NoSuchElementError: No element found using locator - Protractor, with isDisplayed() I get NoSuchElementError: No element found using locator 如何使用Selenium或Protractor获取HTML中嵌套元素的文本以实现自动化? - How do i get the text of a nested element in HTML for automation using Selenium or Protractor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM