Javascript 如何从中获取元素&x27;量角器中的s指数
这是我在这里的第一个问题,我一直在尝试使用量角器进行端到端测试。 我需要从这组代码中获取第一个元素。正如你所看到的,对于这3个元素,我有相同的类和相同的子类。所以我想用索引得到它Javascript 如何从中获取元素&x27;量角器中的s指数,javascript,angularjs,protractor,Javascript,Angularjs,Protractor,这是我在这里的第一个问题,我一直在尝试使用量角器进行端到端测试。 我需要从这组代码中获取第一个元素。正如你所看到的,对于这3个元素,我有相同的类和相同的子类。所以我想用索引得到它 <div class="col-md-4"> <div class="form-group"> <label>Número</label> <input class="form-contro
<div class="col-md-4">
<div class="form-group">
<label>Número</label>
<input class="form-control ng-valid ng-touched ng-dirty" type="number">
</div>
</div>
<div class="form-group">
<label>Inicio Vigência</label>
<input class="form-control ng-untouched ng-pristine ng-valid" placeholder="dd/MM/yyyy" type="date" value="">
</div>
<div class="form-group">
<label>Fim Vigência</label>
<input class="form-control ng-untouched ng-pristine ng-valid" placeholder="dd/MM/yyyy" type="date" value="">
</div>
但它不起作用。量角器不会在输入中输入键。如果需要第一个元素,只需使用
.first()
而不是。获取(2)
:
请注意,您可以使用CSS选择器和:
或者,通过:
$
这里有一个to元素(by.css())
解决此问题的另一种方法是不依赖表单组索引(这听起来很脆弱-想象UI会发生变化,表单控件会简单地重新排列),而是根据其标签定位
input
元素:
var numero = element(by.xpath("//label[. = 'Número']/following-sibling::input"));
numero.sendKeys(aux2);
实际上,这只是一个例子,我在这个组中有10多个元素,这就是为什么我一直在尝试使用get(索引)。@paulotarcio好的,用不同的方法更新了答案来解决这个问题。希望这会有帮助。@Alexe最后一个很好用。谢谢你的帮助。
var numero = element.all(by.className('form-group')).first().all(by.tagName('input'));
numero.sendKeys(aux2);
var numero = $(".form-group:first-of-type input");
numero.sendKeys(aux2);
var numero = $(".form-group:nth-of-type(1) input");
numero.sendKeys(aux2);
var numero = element(by.xpath("//label[. = 'Número']/following-sibling::input"));
numero.sendKeys(aux2);