Here is solution:
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.xerces.impl.xpath.regex.Match;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class exerc3 {
static WebDriver d = null;
public static void main(String[] args) {
d = new FirefoxDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.navigate().to("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
WebElement brand=d.findElement(By.xpath("//*[@id='secondary']/div[1]/div[3]/div"));
List<WebElement>brandlist=brand.findElements(By.tagName("a"));
System.out.println(brandlist.size());
for(int i=0;i<brandlist.size();i++){
System.out.println(i+")"+brandlist.get(i).getText());
}
String parta ="//*[@id='secondary']/div[1]/div[3]/div/ul/li[";
String partb="]/a/span[2]";
int k=1;
int totalvalue=0;
while(brandnumber(parta+k+partb)){
String number=d.findElement(By.xpath(parta+k+partb)).getText();
System.out.println(number);
String n = number.replaceAll( "[^\\d\\-]", "" ); // This line of code is used to replace all special characters in java
System.out.println(n);
int sum=Integer.parseInt(n);
totalvalue=totalvalue+sum;
k++;
}
System.out.println("Total sum of all brands is"+"---"+totalvalue);
String productcount=d.findElement(By.xpath("//*[@id='primary']/div[2]/div[1]/div[1]/span")).getText();
int productstotal=Integer.parseInt(productcount);
if(totalvalue==productstotal){
System.out.println(true);
} else {
System.out.println(false);
}
}
public static boolean brandnumber(String abc){
List<WebElement>a=d.findElements(By.xpath(abc));
if(a.size()==0)
return false;
else
return true;
}
}