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

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

示例代码:

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

我正在 class A 和 Class B 中的页面上创建网络元素列表 我正在尝试单击第一个元素。

执行时出现以下异常:

> java.lang.ClassCastException: java.lang.WhosebugError 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);
    }
}

任何人都可以提出问题所在吗?

我试过 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.WhosebugError 发生在递归调用的情况下。请检查您的代码是否正在发生递归。

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

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

彻底检查代码以找出递归发生的位置。

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

在框架工作中,我们有一个 class 驱动程序,其中 findElements 方法未正确实现,而 findElement 方法已正确实现。因此,问题仅针对 driver.findElements 方法出现,而 driver.findElement 方法运行良好。

感谢大家的帮助!!!