dsidler
dsidler

Reputation: 499

How to use selenium webdriver to extract numeric values from Xpath

Here's the code containing the elements I need to test.

<div class="title ng-binding">     104 Alerts                        </div>
<ul role="menu">
<!-- ngRepeat: category in categories -->

<li class="ng-scope" role="menuitem" ng-click="showAlerts(category.name)" ng-repeat="category in categories" style="">
    <span class="cat-name ng-binding">conservation </span>
    <div class="count ng-binding">44</div>
</li>
<!-- end ngRepeat: category in categories -->
<li class="ng-scope" role="menuitem" ng-click="showAlerts(category.name)" ng-repeat="category in categories">
    <span class="cat-name ng-binding">marketing </span>
    <div class="count ng-binding">3</div>
</li>
<!-- end ngRepeat: category in categories -->
<li class="ng-scope" role="menuitem" ng-click="showAlerts(category.name)" ng-repeat="category in categories">
    <span class="cat-name ng-binding">outstanding delivery requirements </span>
    <div class="count ng-binding">34</div>
</li>

The top element tells us there are 104 Alerts total. Each element below is a certain type of alert. I need to pull out the values in all the alert subcategory elements(44, 3, 34), and Assert that they all add up to 104. Obviously in this case they do not.

Upvotes: 0

Views: 3044

Answers (1)

Paras
Paras

Reputation: 3235

As you can see you need to find the numbers which is the innerHtml of all the div with class = count ng-binding so first you need to find all the elements with this class as below:-

int total = 0;
List<WebElement> totalDivs = driver.findElements(By.xpath("//div[@class='count ng-binding']"));

OR

List<WebElement> totalDivs = driver.findElements(By.className("count"));

Once you get all the WebElements then you need to iterate on them and add them out.

for(WebElement we : totalDivs){
    total += Integer.parseInt(we.getText());
}
System.out.println(total);

Output:-

81 // For this example.

Hope it helps!

Upvotes: 2

Related Questions