如何访问表单元素,如 ng重复 用于使用前端angularjs自动化web应用程序 java selenium Web驱动程序 在这个应用程序中,我能够用xpath自动化大部分事情。现在的情况是,我必须做自动生成的测验,其中的问题数量、问题类型等可以根据测验类型而变化。 现在我必须访问重复索引计数的数据。下面给出了相同的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 .
$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 }}" >