import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class exercise2 {
public static void main(String[] args) {
WebDriver d = new FirefoxDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.navigate().to("http://www.espncricinfo.com/england-v-new-zealand-2015/engine/match/743953.html");
String part1="//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[";
String part2="]/td[4]";
WebElement box = d.findElement(By.xpath("//*[@id='full-scorecard']/div[2]/div/table[1]/tbody"));
//List<WebElement>table1list=box.findElements(By.className("dismissal-info"));
List<WebElement>table1list=box.findElements(By.className("bold"));
System.out.println(table1list.size());
int total=0;
for(int i=0;i<table1list.size();i++){
String result=table1list.get(i).getText();
System.out.println(result);
int val=Integer.parseInt(result);
total=val+total;
}
}
}
List<WebElement>table1list=box.findElements(By.className("bold"));
Above statement takes value of [b:5nk4ozvf]Total Runs[/b:5nk4ozvf] as well because it also has className as bold. So total runs is also added in total at the end using this solution.
Use this option:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class exercise2 {
public static WebDriver d;
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.espncricinfo.com/england-v-new-zealand-2015/engine/match/743953.html");
String parta="//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[";
String partb="]/td[4]";
int k=2;
int total=0;
//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[20]/td[4]
while(score(parta+k+partb)){
String score=d.findElement(By.xpath(parta+k+partb)).getText();
//System.out.println(score);
int finalscore=Integer.parseInt(score);
total=total+finalscore;
//System.out.println("total is-->" + total);
k+=2;
}
System.out.println("Final total -->"+total);
}
public static boolean score(String xpathexpression){
List<WebElement> e=d.findElements(By.xpath(xpathexpression));
if(e.size()==0)
return false;
else
return true;
}
}