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");
}
}
enter image description here
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')
enter image description here
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...
CodePudding user response:
This is surprisingly complicated.
The cast operator ((SomeType) someExpr)
) does 3 almost entirely unrelated things. You're confusing a few of them here, I think.
If the SomeType
is a primitive numeric type (int
, char
, double
, float
, long
, byte
, or short
), then casts convert one thing to another.
But that's where the conversion aspect of casts end. In all other cases you're merely telling the compiler: I know that the expression is of the type I am telling you it is. Sometimes, a runtime check is added (which will throw a ClassCastException
if the expression turns out to not be of that type). Sometimes, the compiler just takes your word for it.
The generics part (<WebElement>
) - that part is where the compiler will just take your word for it, and that generates that warning.
But that warning is completely irrelevant in this scenario: The problem isn't that part at all. The non-generics part (the List
), that gets a runtime check. Because it gets a runtime check, the compiler won't warn: It's fine - if your type assertion turns out to be wrong, at runtime you'll get an exception that'll inform you about this.
So, the warning is about the <WebElement>
part, but the error you get at runtime is about the List
part.
Your WebElement is not a list. Perhaps you are attempting to create a list containing a single element. In that case, you'd want List.of(ele);
. As I said, type-assertion mode casts do not convert anything, so you can't use it to convert an element to a list of just that element.
CodePudding user response:
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));