java.lang.ClassCastException 同时从 Dynamic Webtable 获取行数和列数

java.lang.ClassCastException while Fetching number of rows and columns from Dynamic Webtable

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.WebElement;

    public class WebTable {


    public static void main(String args[])throws ParseException{
    WebDriver wd;
    System.setProperty("webdriver.chrome.driver","E:\Selenium-2017\chromedriver_win32\chromedriver.exe");

    wd = new ChromeDriver();
    wd.get("http://money.rediff.com/gainers/bsc/daily/groupa");

    List<WebElement> col = (List<WebElement>) wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));
    System.out.println("No of cols are : " +col.size());
     List<WebElement> rows = (List<WebElement>) wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr[1]/td[1]"));
     System.out.println("No of rows are :" +rows.size());
     wd.close();
    }
    }

低于异常:

Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 44959
Only local connections are allowed.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.util.List
    at WebTable.main(WebTable.java:17)

findElement returns a WebElement,无法转换为 WebElements 列表。删除强制转换 (List<WebElement>) 并使用 findElements()(带有 's')。

看到这个SO question and this tutorial

这是您问题的答案:

您必须注意以下几件事:

  1. 您正在尝试 findElement(),就像在 wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th")); 中那样,这将 return 只有 一个 WebElement。接下来,您尝试将 WebElement 转换为 List<WebElement>,Java 编译器将其抱怨为 Type safety: Unchecked cast from WebElement to List<WebElement>。请参阅下面的快照:

  1. 因此,我们将使用 findElements() 方法并直接创建一个 List<WebElement>,而不是像 (List<WebElement>) wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th")); 中那样进行转换:

    List<WebElement> col = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));
    
  2. 我们也将对 List<WebElement> rows 采用相同的方法,但我们必须更改 xpath 以获得 rows 的计数,如下所示:

    List<WebElement> rows = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td/a"));
    
  3. 因此您的最终代码块将如下所示:

    package demo;
    
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class Q45393541_rediff {
    
        public static void main(String[] args) {
    
    
            WebDriver wd;
            System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
            wd = new ChromeDriver();
            wd.get("http://money.rediff.com/gainers/bsc/daily/groupa");
            List<WebElement> cols = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));
            System.out.println("No of cols are : " +cols.size());
            List<WebElement> rows = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td/a"));
            System.out.println("No of rows are :" +rows.size());
            wd.close();
    
        }
    
    }
    
  4. 控制台上的输出为:

    No of cols are : 5
    No of rows are :182
    

如果这回答了您的问题,请告诉我。