如何使用java-selenium webdriver访问angularjs表单元素?

时间:2018-05-29 08:43:15

标签: angularjs selenium selenium-webdriver selenium-chromedriver

如何使用 java-selenium Webdriver 访问带有前端angularjs的Web应用程序自动化 ng-repeat 等表单元素。在xpath的帮助下,我能够使用它自动化Web应用程序中的大部分内容。现在情况来了,我必须做自动生成的测验,其中问题的数量,问题类型等可以根据测验类型而变化。 现在我必须访问data-ng-repeat索引计数。下面给出了相同的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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

爪哇

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);
}

答案 1 :(得分:1)

如果您尝试获取$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 }}"
>