这是我的第一个问题,我一直在尝试使用Protractor进行端到端测试。我需要从这组代码中获取第一个元素。如您所见,这3个元素具有相同的类和相同的子类。所以我想通过使用索引来获取它。
<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>
我正在尝试:
var numero = element.all(by.className('form-group')).get(2).all(by.tagName('input'));
numero.sendKeys(aux2);
但这不起作用。量角器没有在输入中发送键。
好吧,如果您需要第一个元素,只需使用.first()
代替.get(2)
:
var numero = element.all(by.className('form-group')).first().all(by.tagName('input'));
numero.sendKeys(aux2);
请注意,您可以使用CSS选择器和first-of-type
伪类一次性定位所需的输入元素:
var numero = $(".form-group:first-of-type input");
numero.sendKeys(aux2);
或者,通过nth-of-type()
:
var numero = $(".form-group:nth-of-type(1) input");
numero.sendKeys(aux2);
$
这里是一个方便的捷径来element(by.css())
。
解决此问题的另一种方法是不依赖表单组索引(实际上听起来很脆弱-假设UI会发生变化,并且表单控件将简单地重新排列),而是input
根据其标签定位元素:
var numero = element(by.xpath("//label[. = 'Número']/following-sibling::input"));
numero.sendKeys(aux2);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句