我想取数据并将其组织为不带标签。它看起来是这样的在java中使用xpath和selenium解析HTML表格数据
<table class="SpecTable">
<col width="40%" />
<col width="60%" />
<tr>
<td class="LightRowHead">Optical Zoom:</td>
<td class="LightRow">15x</td>
</tr>
<tr>
<td class="DarkRowHead">Digital Zoom:</td>
<td class="DarkRow">6x</td>
</tr>
<tr>
<td class="LightRowHead">Battery Type:</td>
<td class="LightRow">Alkaline</td>
</tr>
<tr>
<td class="DarkRowHead">Resolution Megapixels:</td>
<td class="DarkRow">14 MP</td>
</tr>
</table>
,我希望能够提取的信息,所有的字符串,这样我可以与眼前这个的纯文本文件存储:
光学变焦:15倍数码变焦:6X电池类型:碱性分辨率 万像素:14万像素
public static void main(String[] args) {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);
String Url = "http://www.walmart.com/ip/Generic-14-MP-X400-BK/19863348";
driver.get(Url);
List<WebElement> resultsDiv = driver.findElements(By.xpath("//table[contains (@class,'SpecTable')//td"));
System.out.println(resultsDiv.size());
for (int i=0; i<resultsDiv.size(); i++) {
System.out.println(i+1 + ". " + resultsDiv.get(i).getText());
}
我编程在Java中使用Selenium和我想不通正确的XPath表达式。
有人能弄清楚为什么我犯这个错误,也许给我一些关于如何正确解析这些数据的指针?我对Selenium和XPath非常新,但我需要这个工作。
此外,如果任何人有任何良好的来源,我学习Selenium和XPath快,那些也将不胜感激!
这是一个Java问题,而不是一个问题CSHARP。 – Horcrux7 2012-10-11 18:29:00