2017-07-29 49 views
-1
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(); 
    } 
    } 

获得以下异常:java.lang.ClassCastException同时读取行数和列数从动态网表

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) 

回答

0

findElement返回WebElement,不能转换为WebElements的名单。取消铸造(List<WebElement>)并使用findElements()(用's')代替。

看到这个SO questiontutorial

+0

谢谢安德烈。它在加入“s”之后起作用 –

0

这里是回答你的问题:

你必须按如下方式在这里照顾一对夫妇的事情:

  1. 您正在尝试findElement()wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));一样,它将只返回一个WebElement。接下来,您试图将WebElement转换为List<WebElement>,Java编译器正在抱怨,因为Type safety: Unchecked cast from WebElement to List<WebElement>。见下文快照:

enter image description here

  • 因此,而不是铸造如(List<WebElement>) wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));我们将使用findElements()方法和直接创建List<WebElement>如下:

    List<WebElement> col = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th")); 
    
  • 我们也会采取与List<WebElement> rows相同的方法,但我们必须更改xpath以得到的计数10如下:

    List<WebElement> rows = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td/a")); 
    
  • 因此,最终的代码块看起来像:

    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(); 
    
        } 
    
    } 
    
  • 输出在控制台上会是:

    No of cols are : 5 
    No of rows are :182 
    
  • 让我知道如果这个答案是你的问题。

    +0

    谢谢! Debanjan。添加“s”后,我越来越想要o/p.Yes它适用于我 –

    +0

    @karishmayadav这不仅是关于“添加”s“'。你必须删除铸造&构建一个独特的'xpath' findElements()'工作:)谢谢 – DebanjanB

    +0

    我也这么做:-)谢谢 –