【发布时间】:2021-10-11 11:16:30
【问题描述】:
我的代码:- 文件1.java
public int isListAvailable(String locator) throws Exception {
WebElement ele = getObject(locator);
List<WebElement> ls = **(List<WebElement>) ele;**
//List<WebElement> ls1 = driver.findElements((By) ele);
//List<WebElement> ls2 = driver.findElements(ele);
int rowCount = ls.size();
System.out.println("Last1 row=" + rowCount);
return rowCount;
}
public WebElement getObject(String locatorKey) throws Exception {
WebElement ele = null;
WebDriverWait wait = new WebDriverWait(driver, 10);
try {
if (locatorKey.endsWith("_xpath")) {
ele = driver.findElement(By.xpath(prop.getProperty(locatorKey))); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(prop.getProperty(locatorKey))));
}
.....
...
....
}catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return ele;
}
File2.java(catlisting_xpath 是元素的 XPATH)
public void search_List() throws Exception {
if(con.isListAvailable("catlisting_xpath") >=1)
{
con.infoLog("Category List is available");
}else {
con.infoLog("Category List is not available");
}
}
enter image description here
错误:-
java.lang.ClassCastException:类 org.openqa.selenium.remote.RemoteWebElement 无法转换为类 java.util.List(org.openqa.selenium.remote.RemoteWebElement 在加载程序“app”的未命名模块中;java .util.List 位于加载器“bootstrap”的模块 java.base 中)
enter image description here
问题是,当我在 File1.java 上方运行或键入时,在 List ls = (List) ele; 处收到警告。 警告是 类型安全:从 WebElement 到 List 的未经检查的强制转换
谁能帮帮忙,如何解决这个问题...
【问题讨论】:
标签: java selenium runtime-error typecasting-operator