简体   繁体   中英

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 BI 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?

I tried the XPATH as follows:

List<WebElement> itemList = driver.findElements(By.xpath(".//*[@class='itemrow button']/span[2]"));  // .//*[@class='itemrow button']/span[2]
    System.out.println("list " + itemList);

returned the following elements:

list [[[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]], [[ChromeDriver: chrome on XP (a92615f2a2338af92c3dc57ff2366bf8)] -> xpath: .//*[@class='itemrow button']/span[2]]]

I hope it is not something problem with the XPATH or findElements method. Are you using any framework for writing tests?

In the line:

 //itemList = (driver.findElements(By.xpath(".//*[@id='hc6|stocks|item1']/span[2]"")));

observed additional double quotes present at the end of xpath span[2]"" . check if it is the reason for the error.

java.lang.StackOverflowError occurs in case of recursive calls made. please look into your code whether recursion is happening.

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

In the catch block, you used Exception , but code throws the StackOverflowError which is an Error (but not an Exception)

Go through the code thoroughly to find out where the recursion is happening.

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!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM