gpt4 book ai didi

java - Webdriver异常:Double cannot be cast to java. lang.String

转载 作者:行者123 更新时间:2023-12-01 22:52:44 25 4
gpt4 key购买 nike

我尝试执行下面粘贴的 webdriver 代码,其中用户名字段为 String,付款字段类型为 double。它执行时带有粘贴在下面的错误消息。

我可以知道如何克服这个问题

错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String

Webdriver Java 代码:

package exceltest;

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

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class sample {

public static void main( String[] args) throws Exception {
String [][] data;
data = excelread();

String expectedtitle;
for (int i = 1; i < data.length; i++ )
{

expectedtitle = Paymentdetails(data[i][0],data[i][1]);

System.out.println("page title after login is" + expectedtitle );

if(expectedtitle.equalsIgnoreCase("homepage Title"))
{

System.out.println("PASSED");
}

else{
System.out.println("FAILED");

}

}
}







public static String Paymentdetails(String username,String payment) throws InterruptedException {
WebDriver driver = new FirefoxDriver();

driver.get("http://localhost/xxx/Default.aspx");

Thread.sleep(1000);
//driver.findElement(By.id("LoginUserName")).clear();
driver.findElement(By.id("LoginUserName")).sendKeys(username);
//driver.findElement(By.id("LoginPayment")).clear();


driver.findElement(By.id("LoginPayment")).sendKeys(payment);
driver.findElement(By.id("LoginLoginButton")).click();
Thread.sleep(1000);
String expectedtitle = driver.getTitle();
return expectedtitle;

}








public static String [][] excelread()throws Exception
{
File excel = new File("D:\\Book1.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet("Sheet1");

int totrow = ws.getLastRowNum()+ 1;
int totcol = ws.getRow(0).getLastCellNum();
String [][] data = new String[totrow][totcol];

for (int i = 0 ; i < totrow ; i++) {
XSSFRow row = ws.getRow(i);
for (int j=0 ; j < totcol ; j++){
XSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println("The value is " + value);



}
}
return data;
}


public static String cellToString(XSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue();
break;
case 1 :
result = cell.getStringCellValue();
break;
default :
throw new RuntimeException("There are no support for this type of cell");
}
return (String) result();
}
}

最佳答案

对于您现有的解决方案,请尝试此操作,

在 cellToString() 中将 return (String) result(); 替换为 return result.toString(); 并且它将起作用。

目前您正在尝试将 java.lang.Double 转换为 java.lang.String当您将基元分配给对象引用时,编译器会隐式地将其装入其包装类中。要测试此功能,请执行以下测试用例:

public static void main(String[] args) {
Object o = null;
double d = 10d;
o = d;
if (o instanceof Double) {
System.out.println("instance of java.lang.Double");
} else {
System.out.println("not an instance of Double");
}
String dStr = o.toString();
System.out.println(dStr);
}

这就是为什么结果变成 java.lang.Double 并且您不能直接将其转换为字符串,而应该返回它的字符串值。

事实上你的cellToString()应该看起来像这样:

public static String cellToString(XSSFCell cell) {
String result = null;
int type = cell.getCellType();
switch (type) {
case 0:
result = String.valueOf(cell.getNumericCellValue());
break;
case 1:
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException(
"There are no support for this type of cell");
}
return result;
}

关于java - Webdriver异常:Double cannot be cast to java. lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com