使用xpath选择某个下拉元素(或更好的替代)

时间:2016-10-27 19:12:05

标签: javascript selenium protractor

如何在此下拉列表中选择某个元素。

<li role="presentation" ng-repeat="match in $matches" ng-class="{active: $isActive($index)}" class="ng-scope active">
    <a style="cursor: default" role="menuitem" tabindex="-1" ng-click="$select($index, $event)">
        <!-- ngIf: $isMultiple && $isActive($index) -->
            <i class="glyphicon glyphicon-ok pull-right" ng-if="$isMultiple &amp;&amp; $isActive($index)"></i>
        <!-- end ngIf: $isMultiple && $isActive($index) --> 
        <span ng-bind="match.label" class="ng-binding">Streamer</span>
    </a>
</li> 

我试过这个

element(by.model('selectedAvailable')).click();
element(by.xpath('..//ul//li[1]')).click().

和此:

element(by.repeater('match in $matches').row(0)).click();

2 个答案:

答案 0 :(得分:2)

另一种方法是使用by.cssContainingText

element(by.cssContainingText('[ng-repeat="match in $matches"]', 'Streamer')).click();

答案 1 :(得分:1)

我会filter(),假设你知道&#34; Streamer&#34;标签并想要选择它:

var matches = element.all(by.repeater('match in $matches')); 
matches.filter(function (match) {
    return match.element(by.binding("match.label")).getText().then(function (text) {
        return text === "Streamer";
    });
}).first().click();

或者,与evaluate()代替getText()的方式类似:

var matches = element(by.repeater('match in $matches')); 
matches.filter(function (match) {
    return match.evaluate("match.label").then(function (label) {
        return label === "Streamer";
    });
}).first().click();