Getting key and respective value in ng-repeat using Selenium and Python



  • Being new to Selenium, I have been trying to figure how to extract the value corresponding to the key under the ng-repeat, in my case, the key is 'status'.

    The partial page is as follow:

    <div ng-repeat="(key, value) in lastResult" class="ng-scope">
                  <div class="row">
                    <div class="col-xs-4">
                      <span class="key pull-right ng-binding">status</span>
                    </div>
                    <div class="col-xs-8">
                      <span class="value ng-binding" ng-class="isUrl(value) ? 'ellipsis' : ''" ng-bind-html="value | highlight">1</span>
                    </div>
                  </div>
                </div>
    
    <div ng-repeat="(key, value) in lastResult" class="ng-scope">
                  <div class="row">
                    <div class="col-xs-4">
                      <span class="key pull-right ng-binding">type</span>
                    </div>
                    <div class="col-xs-8">
                      <span class="value ng-binding" ng-class="isUrl(value) ? 'ellipsis' : ''" ng-bind-html="value | highlight">standard</span>
                    </div>
                  </div>
                </div>
    

    Page can contain many other ng-repeats, but I am only interested in the value of the 'status' which is '1' in the above sample code.

    Please help! I am using selenium with python 3. Thank you



  • The idea here is to apply the findAll - filter - extract pattern.

    Find all:

    ng_scopes = driver.find_elements_by_class("ng-scope")
    

    Filter:

    ng_scopes_with_status_one = [ng_scope for ng_scope in ng_scopes if ng_scopes.find_element_by_css_selector(".col-xs-8 span").text == "1"]

    Extract:

    ng-repeat_value = ng_scopes_with_status_one[0].get_attribute("ng-repeat")
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2