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)
答案 0 :(得分:0)
findElement
返回WebElement
,无法转换为WebElements列表。移除广告(List<WebElement>)
并使用findElements()
(使用&#39; s&#39;)代替。
请参阅此SO question和此tutorial
答案 1 :(得分:0)
以下是您的问题的答案:
您必须按照以下方式处理以下几项事项:
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>
。见下面的快照:因此,我们将使用(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
以获得rows
的计数,如下所示:
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
如果这回答你的问题,请告诉我。