Java.lang.NumberFormatException while String to int convert | Selenium Forum
M
Posted on 29/01/2017
Question in Module 13 exercise:

[b:3k6s8157]Go to http://www.espncricinfo.com/india-v-england-2016-17/engine/match/1034825.html
Add up runs scored by each player
Check whther its equal to total runs[/b:3k6s8157]

I made a list and can access scores on webpage, then converted it to String array.

But not able to convert String array to int array and giving java.lang.NumberFormatException while running the program.

Please refer attachment and suggest solution on how to add up the scores.

M
Replied on 29/01/2017

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;

}



}

}


M
Replied on 11/02/2017

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.


M
Replied on 12/02/2017

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;


}

}