我想取數據並將其組織爲不帶標籤。它看起來是這樣的在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