Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误:无法从Selenium中的数字单元格中获取字符串值_Java_Excel_Selenium_Selenium Webdriver - Fatal编程技术网

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

Java 错误:无法从Selenium中的数字单元格中获取字符串值,java,excel,selenium,selenium-webdriver,Java,Excel,Selenium,Selenium Webdriver,我试图从Excel文件导入数据以自动处理大量数据,但出现错误“无法从数字单元格获取字符串值”,因为某些单元格具有数值,而其他单元格具有文本值。我需要提取所有数据并将其作为页面字段的输入插入。我从你那里得到了这个错误 signin_credentials[i][2] = configuration.getData(0, i, 5); signin_credentials[i][3] = configuration.getData(0, i, 6); 这是我的密码: import org.apa

我试图从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;
    }
}

改进的方法
getData(int sheetnumber,int row,int column)
始终以
字符串或
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;
}

改进的方法
getData(int sheetnumber,int row,int column)
始终以
字符串或
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;
}
尝试
((xssfcell)sheet.getRow(row).getCell(column)).getRawValue()
这对我来说有着不同的作用 您实际上不需要使用多个if条件。

尝试
((xssfcell)sheet.getRow(row.getCell(column)).getRawValue()
这对我来说有着不同的作用
您实际上不需要使用多个if条件。

非常感谢pburgr的帮助和努力,它非常有效。如果你允许的话,有没有办法只登录一次并重复插入数据。我不明白你的问题。在您的代码中,我看不到登录和任何数据插入之间的任何联系。非常感谢,一切正常。请将答案标记为有用。谢谢。非常感谢你的帮助和努力,它工作得非常好。如果你允许的话,有没有办法只登录一次并重复插入数据。我不明白你的问题。在您的代码中,我看不到登录和任何数据插入之间的任何联系。非常感谢,一切正常。请将答案标记为有用。谢谢