尝试访问列表中的元素时获取java.lang.ClassCastException

时间:2016-11-08 13:14:30

标签: java selenium selenium-webdriver classcastexception

示例代码:

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();
    }
}

我正在A类和B类页面上创建webelements列表我试图点击第一个元素。

执行时我得到以下异常:

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

问题必须对findElements做一些事情,因为当我在代码中手动使用findElement方法将元素添加到列表中时,代码工作正常。

在下面的示例中,我已经评论了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);
    }
}

有谁能说明什么出错?

2 个答案:

答案 0 :(得分:0)

我按照以下方式尝试了XPATH:

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

返回以下元素:

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

我希望它不是XPATH或findElements方法的问题。您是否正在使用任何框架来编写测试?

在行中:

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

观察到xpath span[2]""末尾出现的其他双引号。检查是否是错误的原因。

java.lang.StackOverflowError在发生递归调用的情况下发生。请查看您的代码是否正在进行递归。

  

java.lang.ClassCastException:java.lang.StackOverflowError不可以   强制转换为java.lang.Exception

在catch块中,您使用了Exception,但是代码抛出StackOverflowError这是一个错误(但不是异常)

彻底浏览代码,找出递归发生的位置。

答案 1 :(得分:0)

问题不在代码中。问题出在框架中。

在框架工作中,我们有一个驱动程序类,其中未正确实现findElements方法,正确实现了findElement方法。因此,问题只出现在driver.findElements方法和driver.findElement方法正常工作。

感谢大家的帮助!!!!