如何使用由$ index的ng-repeat track创建的量角器选择元素? [英] How to select elements using protractor created by ng-repeat track by $index?

查看:82
本文介绍了如何使用由$ index的ng-repeat track创建的量角器选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择ng-repeat创建的文本框,并使用sendKeys函数发送一些值.但是我不确定选择文本框的方法.请提出一种完成此操作的方法,或者我应该改用css选择器.

I need to select the textbox created by ng-repeat and send some values using sendKeys Function. But I'am not sure about the method to select the textboxes. Please suggest a method to accomplish this Or Should I use css selectors instead.

<div class="qst_input_hld ng-scope" ng-repeat="option in options track by $index">
<input type="text" class="input-sm ng-pristine ng-valid" ng-model="options[$index]" placeholder="Option 1" ng-class="isOptionEmpty[$index] ? 'error-border' : ''">
<!-- ngIf: $index > 1 -->
</div>

推荐答案

有多种方法可以定位文本输入,并且由于那里有中继器,我怀疑有多个文本框.假设您想将密钥发送到第一个密钥,这是一个选项:

There are multiple ways to locate the text input and, since there is a repeater there, I suspect there are multiple text boxes. Assuming you want to send keys to the first one, here is one option:

var desiredInput = element.all(by.repeater("option in options")).first().all(by.tagName("input")).first();
desiredInput.sendKeys("desired text");

请注意,您根本不需要处理track by部分-量角器(还请注意,我只是使用了by.tagName()技术,该技术可能有效也可能无效,这取决于您是否还有其他input元素.您可以更严格一些,并使用 CSS选择器代替,例如检查占位符:

Also note that I've just used the by.tagName() technique which may or may not work depending on if you have the other input elements there. You can be more strict and use a CSS selector instead, e.g. check the placeholder:

var desiredInput = element.all(by.repeater("option in options")).first().$('input[placeholder="Option 1"]');

并且,如果您想将密钥发送到转发器中每个项目的输入元素,请使用 each() :

And, if you want to send keys to input element for every item in the repeater, use each():

element.all(by.repeater("option in options")).each(function (elm) {
    var desiredInput = elm.$('input[placeholder="Option 1"]');
    desiredInput.sendKeys("desired text");
});

这篇关于如何使用由$ index的ng-repeat track创建的量角器选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆