Cannot get a STRING value from a NUMERIC cell : This is the error i get when i try to get data from Excel using selenium Java how to solve this

Cannot get a STRING value from a NUMERIC cell : This is the error i get when i try to get data from Excel using selenium Java how to resolve

i am a beginner this is the code i used

package testcases;

import org.testng.annotations.Test;

import testutils.ExcelDataRead;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;

public class Excel_reading_register_demo2 {
    WebDriver driver;
    WebDriverWait wait;
    
    public ExcelDataRead excelObj;
    String xlFilePath ="testdata//excel_reading_register_demo2.xlsx";
    String sheetName = "Sheet1";
    
    @DataProvider(name="userData")
     @Test(priority = 2)
    public Object[][] userFormData() throws Exception
    {
        Object[][] data = testData(xlFilePath, sheetName);
        return data;
    }
    
    public Object[][] testData(String xlFilePath, String sheetName) throws Exception//User Defined Method
    {
        excelObj = new ExcelDataRead(xlFilePath);
        int rows = excelObj.getRowCount(sheetName);
        int columns = excelObj.getColumnCount(sheetName);
        Object[][] excelData = new Object[rows-1][columns]; 
        for(int i=1; i<rows; i++)
        {
            for(int j=0; j<columns; j++)
            {
                excelData[i-1][j] = excelObj.getCellData(sheetName, i, j);
            }
        }
        return excelData;
    }
    
     @Test(dataProvider = "userData",priority = 1)
      
        public void f(String firstName, String lastName, CharSequence[] phoneNum, String gmail, String address,  
                String cityN, String stateN, CharSequence[] postcodeN,String userName, String passWord, String passWord1) throws
        InterruptedException
        {
         driver.findElement(By.linkText("REGISTER")).click(); 
            
            driver.findElement(By.name("firstName")).sendKeys(firstName);
            driver.findElement(By.name("lastName")).sendKeys(lastName);
            driver.findElement(By.name("phone")).sendKeys(phoneNum);
            driver.findElement(By.name("userName")).sendKeys(gmail);
            driver.findElement(By.name("address1")).sendKeys(address);
            driver.findElement(By.name("city")).sendKeys(cityN);
            driver.findElement(By.name("state")).sendKeys(stateN);
            driver.findElement(By.name("postalCode")).sendKeys(postcodeN);
            
            driver.findElement(By.name("email")).sendKeys(userName);
            driver.findElement(By.name("password")).sendKeys(passWord);
            driver.findElement(By.name("confirmPassword")).sendKeys(passWord1);
            
            driver.findElement(By.name("submit")).click();
            
            Thread.sleep(7000);
            
            driver.findElement(By.linkText("REGISTER")).click();
        }
     
  
  @Parameters("browser")
  @BeforeTest
  public void beforeTest(String browser) {
      
      if(browser.equals("firefox"))
      {
      System.setProperty("webdriver.gecko.driver","drivers\\geckodriver.exe");
      driver=new FirefoxDriver();
      }
      else if(browser.equals("chrome"))
      { 
      System.setProperty("webdriver.chrome.driver","drivers\\chromedriver.exe");
      driver=new ChromeDriver();
      }
     
      driver.get("http://demo.guru99.com/test/newtours/");
      wait=new WebDriverWait(driver,20);
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }

  @AfterTest
  public void afterTest() {
      
      driver.close();
      
  }

}

This is the error which i get :

3 Likes

You can try 2 approaches here.

Change the data in the Excel as indicated here

Change to code to validate for string data, as indicated here

3 Likes

When debugging your code, try and break it down, starting with the stack trace and line number to figure out what the issue is.

In this case, the exception tells you the problem “Cannot get a STRING value from a NUMERIC cell”, so it’s a type issue. The cell is formatted as numeric in the spreadsheet, while the code is expecting strings.

While it’s not clear what testutils.ExcelDataRead is based on, you can probably improve the getCellData() method in it to handle different column types from Excel, rather that it assuming that everything will be string data.

1 Like

**guys the error is SOLVED what i did was i changed CharSequence phoneNum to String phoneNum and CharSequence postcodeN to String postcodeN in simple words i just changed the CharSequence to String that’s it Thank you for all your Answers !!! **

1 Like