【发布时间】:2012-05-06 14:56:56
【问题描述】:
我想获取数据并在没有标签的情况下对其进行组织。它看起来像这样
<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 倍数码变焦:6 倍电池类型:碱性分辨率 百万像素:14 MP
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 和 XPaths 很陌生,但我需要这个来工作。
另外,如果有人有任何好的资源让我快速学习 Selenium 和 XPath,我们也将不胜感激!
【问题讨论】:
标签: java html xpath selenium webdriver