Akash Chavan Akash Chavan - 4 days ago 6
Java Question

Getting java.lang.ClassCastException while trying to access element in the list

Sample Code:

public class A
{
List<WebElement> itemList = new ArrayList<WebElement>();
public List<WebElement> getItemsList()
{
itemList = (driver.findElements(By.xpath("<some valid xpath>")));
return(itemList);
}
}

public class B
{
A hp = new A();
public void subscribe()
{
hp.getItemsList().get(0).click();
}
}


I am creating the list of webelements on the page in the class A and in the Class B I am trying to click on the first element.

On execution I am getting below exception:

> java.lang.ClassCastException: java.lang.StackOverflowError cannot be cast to java.lang.Exception


Issues has to do some thing with findElements because when I added the elements using findElement method to the list manually in the code, code is working fine.

In the below example I have commented the findElements line and instead added the elements manually, this code is working fine.

public class A
{
List<WebElement> itemList = new ArrayList<WebElement>();

public List<WebElement> getItemsList()
{
//itemList = (driver.findElements(By.xpath(".//*[@id='hc6|stocks|item1']/span[2]"")));
itemList.add(driver.findElement(By.xpath(".//*[@id='hc6|stocks|item1']/span[2]")));
itemList.add(driver.findElement(By.xpath(".//*[@id='hc6|stocks|item2']/span[2]")));
itemList.add(driver.findElement(By.xpath(".//*[@id='hc6|stocks|item3']/span[2]")));
return(itemList);
}
}


Can anyone suggests whats going wrong?

Answer

The issue was not in the code. Issues was in the framework.

In frame work we have a class for driver where findElements method was not properly implemented, where as findElement method was properly implemented. Hence the issue was coming up only for the driver.findElements method and driver.findElement method was working fine.

Thanks all for the Help!!!!

Comments