简体   繁体   中英

How to get values of same class/properties in Selenium C#?

I am currently automating tests to compare expected and actual endorsements on a summary page.

How can I read all endorsements values on the page shown on the summary page.These can change, meaning there can be 2-5 depending on different input. I have tried Xpath and CSS selector but have had no luck. Here are the elements properties for two endorsements the rest of the endorsements will have same properties (elements wise) just different values.

I want to be able to get all the endorsements listed on the page so I can input to my excel sheet for comparison against expected endorsement.

ENDORSEMENT 1:

         <div class="guidance smaller ng-scope" ng-repeat="end in 
          prop.Endorsements">
          <a ng-href="#c03770af-3724-4c3a-a240-e341c0d2c3ef" ng-bind-
          html="end.Name" class="ng-binding" href="#c03770af-3724-4c3a-
          a240-e341c0d2c3ef">Restricted Theft</a>
          </div>
          <a ng-href="#c03770af-3724-4c3a-a240-e341c0d2c3ef" ng-bind-
          html="end.Name" class="ng-binding" href="#c03770af-3724-4c3a-a240-
          e341c0d2c3ef">Restricted Theft</a>

ENDORSEMENT 2:

       <div class="guidance smaller ng-scope" ng-repeat="end in 
       prop.Endorsements">
       <a ng-href="#93ff9067-f64c-4879-933d-8b0a1d077e74" ng-bind-
        html="end.Name" class="ng-binding" href="#93ff9067-f64c-4879-933d-
        8b0a1d077e74">Malicious Damage Exclusion</a>
        </div>
         <a ng-href="#93ff9067-f64c-4879-933d-8b0a1d077e74" ng-bind-
         html="end.Name" class="ng-binding" href="#93ff9067-f64c-4879-933d-
         8b0a1d077e74">Malicious Damage Exclusion</a>

You need a XPath expression to catch all the a elements at once and store them in a list.

When there are no other anchor tags then the Endorsements:

IList<IWebElement> listOfEndorsements= Driver.FindElements(By.XPath("//a"));

When there are other kind of anchor tags you can try:

IList<IWebElement> listOfEndorsements= Driver.FindElements(By.XPath("//div[contains(@ng-repeat,'prop.Endorsements')]/a"));

Then you can use a ForEach loop to extract from the list of IWebElements the information you need.Like:

foreach (var endorsement in listOfEndorsements)
{
    var text = endorsement.Text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM