My Code:- File1.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 is XPATH of the elements)
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");
}
}
ERROR:-
java.lang.ClassCastException: class org.openqa.selenium.remote.RemoteWebElement cannot be cast to class java.util.List (org.openqa.selenium.remote.RemoteWebElement is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
the issue is, while I run or type this above the File1.java have got a warning at List ls = (List) ele; Warning is Type safety: Unchecked cast from WebElement to List
can anyone help out, how to solve this...
WebElement ele = getObject(locator);
List<WebElement> ls = (List<WebElement>) ele;
You are casting List
to WebElement
and that is not valid. I believe in your getObject
method you are using findElement
method to identify the locator. Instead of that use findElements
method to get List
of WebElement
Try like below,
List<WebElement> ls = driver.findElements(By.xpath(locator));