2

How can I access the form elements like ng-repeat for automating web applications with front end angularjs using java-selenium Webdriver. I was able to automate most of the things in the web application with this with the help of xpath. Now situation came where I have to do auto generated quizzes where the number of questions, the question type, etc can vary depending upon the quiz type. Now I have to access the data-ng-repeat index count. The html of the same is given below, please help me to get the index count here.

<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>

2
  • Please add script language tag ,like java, javascript, python Commented May 29, 2018 at 9:34
  • Are you trying to get the value of $index? Commented May 29, 2018 at 10:48

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's not going to work for a production build where you can't access .scope().
1

If you are trying to get the value of $index change your ng-repeat to include a data- attribute with the value. Have a look at 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 }}"
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.