我怎样才能访问像ng-repeat这样的表单元素,以使用java-selenium Webdriver使用前端angularjs自动化Web应用程序。在 xpath 的帮助下,我能够用这个自动化 Web 应用程序中的大部分事情。现在情况来了,我必须进行自动生成的测验,其中问题的数量、问题类型等可能会因测验类型而异。 现在我必须访问数据 ng 重复索引计数。下面给出了相同的html,请帮助我在此处获取索引计数。
<div class="margin-top-30">
<!-- <div class="progress-box time-header-font-2 text-center open-sans"
ng-repeat="x in [1,2,3,4,5,6,7] track by $index">{{x}}
</div> -->
<!-- ngRepeat: assessment in assessmentData -->
<div data-ng-repeat="assessment in assessmentData" class="progress-box time-header-font-2 text-center pointer-cursor open-sans legend-0" data-ng-click="loadRandomQuestion($index)" style="">1</div>
<!-- end ngRepeat: assessment in assessmentData -->
<div data-ng-repeat="assessment in assessmentData" class="progress-box time-header-font-2 text-center pointer-cursor open-sans legend-0" data-ng-click="loadRandomQuestion($index)">2</div>
<!-- end ngRepeat: assessment in assessmentData -->
<div data-ng-repeat="assessment in assessmentData" class="progress-box time-header-font-2 text-center pointer-cursor open-sans legend-0" data-ng-click="loadRandomQuestion($index)">3</div>
<!-- end ngRepeat: assessment in assessmentData -->
<div data-ng-repeat="assessment in assessmentData" class="progress-box time-header-font-2 text-center pointer-cursor open-sans legend-0" data-ng-click="loadRandomQuestion($index)">4</div>
<!-- end ngRepeat: assessment in assessmentData -->
<div data-ng-repeat="assessment in assessmentData" class="progress-box time-header-font-2 text-center pointer-cursor open-sans legend-0" data-ng-click="loadRandomQuestion($index)">5</div>
<!-- end ngRepeat: assessment in assessmentData -->
</div>
<div class="col-sm-12 padding-snip">
<div data-ng-show="finish_quiz" class="finish-quiz-btn text-center ng-hide" style="">
<span class="time-header-font-1 open-sans"><span translate="portallang_finishQuiz" class="ng-scope">FINISH QUIZ</span></span>
</div>
<!-- <div ng-if="count+1 >= totalQuestions" class="finish-quiz-btn text-center pointer-cursor legend-1" ng-click="stop();">
<span class="time-header-font-1 open-sans">FINISH QUIZ</span>
</div> -->
</div>
Java
List<WebElement> list = driver.findElements(
By.cssSelector("div[data-ng-repeat='assessment in assessmentData']"));
String script = "angular.element(arguments[0]).scope().$index";
for(WebElement item:list) {
String index = ((JavascriptExecutor) driver).executeScript(script, item).toString();
System.out.println("text: " + item.getText());
System.out.println("index: " + index);
}
如果您尝试获取$index
的值,请更改ng-repeat
以在该值中包含data-
属性。看看ng-attr
.
<div class="progress-box time-header-font-2 text-center open-sans"
ng-repeat="x in [1,2,3,4,5,6,7] track by $index"
ng-attr-data-index="{{ $index }}"
>