[英]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. 我正在A类页面上创建Webelements列表,而在BI类中试图单击第一个元素。
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. 问题与findElements有关,因为当我在代码中手动使用findElement方法将元素添加到列表中时,代码可以正常工作。
In the below example I have commented the findElements line and instead added the elements manually, this code is working fine. 在下面的示例中,我注释了findElements行,而手动添加了元素,此代码运行良好。
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: 我尝试了如下的XPATH:
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. 我希望XPATH或findElements方法没有问题。 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]""
. 观察到xpath span[2]""
的末尾出现了额外的双引号。 check if it is the reason for the error. 检查是否是错误原因。
java.lang.StackOverflowError occurs in case of recursive calls made. 如果进行递归调用,则会发生java.lang.StackOverflowError。 please look into your code whether recursion is happening. 请检查您的代码是否正在进行递归。
java.lang.ClassCastException: java.lang.StackOverflowError cannot be cast to java.lang.Exception java.lang.ClassCastException:无法将java.lang.StackOverflowError强制转换为java.lang.Exception
In the catch block, you used Exception
, but code throws the StackOverflowError
which is an Error (but not an Exception) 在catch块中,您使用了Exception
,但是代码抛出了StackOverflowError
,这是一个Error(但不是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. 在框架工作中,我们有一个驱动程序类,其中未正确实现findElements方法,而未正确实现findElement方法。 Hence the issue was coming up only for the driver.findElements method and driver.findElement method was working fine. 因此,仅对于driver.findElements方法出现了问题,而driver.findElement方法运行正常。
Thanks all for the Help!!!! 谢谢大家的帮助!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.