错误:无法从selenium中的数字单元格中获取字符串值

zdwk9cvp  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(510)

我正试图从excel文件导入数据以自动处理大量数据,但出现错误“无法从数字单元格获取字符串值”,因为某些单元格具有数字值,而其他单元格具有文本值。我需要提取所有数据并将其作为页字段的输入插入。我的错误来自

signin_credentials[i][2] = configuration.getData(0, i, 5);
signin_credentials[i][3] = configuration.getData(0, i, 6);

这是我的密码:

import org.apache.poi.ss.usermodel.CellType;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ExcelExample {
    WebDriver driver;

    @Test(dataProvider = "testdata")
    public void demoClass(String NameEN, String NameAr, String ServiceCode, String min, String max)
            throws InterruptedException {
//System.setProperty("webdriver.chrome.driver", "Path of Chrome Driver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://192.168.1.130/Account/Login");
        driver.findElement(By.name("UsernameOrEmailAddress")).sendKeys("admin");
        driver.findElement(By.name("Password")).sendKeys("P@ssw0rd");
        driver.findElement(By.id("LoginButton")).click();
        Thread.sleep(5000);
        driver.findElement(By.partialLinkText("Services")).click();
        Thread.sleep(500);
//click on service list
        driver.findElement(By.partialLinkText("Service List")).click();
        Thread.sleep(500);
//click on rotate circle
        driver.findElement(By.xpath("/html/body/section[2]/div/div[1]/div/div/div[2]/div[2]/a[4]/span/span")).click();
        Thread.sleep(2000);
//click on +
        driver.findElement(By.xpath("/html/body/section[2]/div/div[1]/div/div/div[2]/div[2]/a[3]/span")).click();
        Thread.sleep(1000);
//write on service name
        driver.findElement(By.id("Name")).sendKeys(NameEN);
        Thread.sleep(500);
//write on service name Arabic
        driver.findElement(By.id("NameAr")).sendKeys(NameAr);
        WebElement drodown = driver.findElement(
                By.xpath("//*[@id=\"ServiceCreateForm\"]/div/div/div/div[2]/div[3]/div[1]/div/div/button"));
        drodown.click();
        WebElement range = driver.findElement(
                By.xpath("//*[@id=\"ServiceCreateForm\"]/div/div/div/div[2]/div[3]/div[1]/div/div/div/ul/li[2]/a"));
        range.click();
//select min amount
        driver.findElement(By.name("MinValue")).sendKeys(min);
//select max amount

        driver.findElement(By.name("MaxValue")).sendKeys(max);
//driver.findElement(By.xpath("//*[@id=\"ServiceCreateForm\"]/div/div/div/div[2]/div[7]/div/button")).click();

    }

    @AfterMethod
    void ProgramTermination() {
//driver.quit();
    }

    @DataProvider(name = "testdata")
    public Object[][] testDataExample() {
        ReadExcelFile configuration = new ReadExcelFile("C:\\Users\\Desktop\\file.xlsx");
        int rows = configuration.getRowCount(0);
        Object[][] signin_credentials = new Object[rows][5];

        for (int i = 0; i < rows; i++) {

            signin_credentials[i][0] = configuration.getData(0, i, 3);
            signin_credentials[i][1] = configuration.getData(0, i, 4);
            signin_credentials[i][2] = configuration.getData(0, i, 5);
            signin_credentials[i][3] = configuration.getData(0, i, 6);

        }

        return signin_credentials;
    }
}

这是我的excel课程:

package Login;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile {
    XSSFWorkbook work_book;
    XSSFSheet sheet;

    public ReadExcelFile(String excelfilePath) {
        try {
            File s = new File(excelfilePath);
            FileInputStream stream = new FileInputStream(s);
            work_book = new XSSFWorkbook(stream);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public String getData(int sheetnumber, int row, int column) {
        sheet = work_book.getSheetAt(sheetnumber);
        String data = sheet.getRow(row).getCell(column).getStringCellValue();

        return data;

    }

    public int getRowCount(int sheetIndex) {
        int row = work_book.getSheetAt(sheetIndex).getLastRowNum();
        row = row + 1;
        return row;
    }
}
ejk8hzay

ejk8hzay1#

尝试 ((xssfcell) sheet.getRow(row).getCell(column)).getRawValue() 这实际上对我起到了不同的作用,你不需要使用多重if条件。

mgdq6dx1

mgdq6dx12#

改进的方法 getData(int sheetnumber, int row, int column) 总是有回报 data 作为一个 String 或者 null :

// add this two imports
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;

...

public String getData(int sheetnumber, int row, int column) {
    sheet = work_book.getSheetAt(sheetnumber);
    XSSFCell cell = sheet.getRow(row).getCell(column);
    String data = null;
    if (cell != null) {
         CellType cellType = cell.getCellType();
         if (cellType.toString() == "STRING") {
            data = cell.getStringCellValue();
         }
         else if (cellType.toString() == "NUMERIC") {
            data = String.valueOf(cell.getNumericCellValue());
         }
         else if (cellType.toString() == "FORMULA") {
            data = cell.getCellFormula();
         }
         else if (cellType.toString() == "BOOLEAN") {
            data = String.valueOf(cell.getBooleanCellValue());
         }
         else if (cellType.toString() == "ERROR") {
             // let data be null or assign some value like this
             data = "ERROR in sheet nr. " + sheetnumber + " row nr. " + row + " column nr. " + column;
         }
         else if (cellType.toString() == "BLANK") {
             // let data be null or assign some value like this
             data = "BLANK cell in sheet nr. " + sheetnumber + " row nr. " + row + " column nr. " + column;
         }
         else {
             // let data be null or assign some value like this
             data = "UNKNOWN cell type in sheet nr. " + sheetnumber + " row nr. " + row + " column nr. " + column;
         }
    }
    return data;
}

相关问题