[英]Parsing HTML table data with xpath and selenium in java
我想获取数据并在没有标签的情况下进行整理。 看起来像这样
<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());
}
我正在使用Selenium在Java中进行编程,因此无法为其找到正确的XPath表达式。
有人可以弄清楚我为什么会犯错,并可能给我一些有关如何正确解析此数据的指示吗? 我对Selenium和XPath很陌生,但我需要这项工作。
另外,如果有人有什么好的资源让我快速学习Selenium和XPath,那么也将不胜感激!
令人惊讶的是, 该规范在XPath上非常不错。
您也可以尝试CSS选择器 。
无论如何,从表中获取数据的一种方法如下:
// gets all rows
List<WebElement> rows = driver.findElements(By.xpath("//table[@class='SpecTable']//tr"));
// for every line, store both columns
for (WebElement row : rows) {
WebElement key = row.findElement(By.XPath("./td[1]"));
doAnythingWithText(key.getText());
WebElement val = row.findElement(By.XPath("./td[2]"));
doAnythingWithText(val.getText());
}
作为另一种选择,您可以将表的所有单元格都放入一个数组并以这种方式访问它们。 例如。
ReadOnlyCollection<IWebElement> Cells = driver.FindElements(By.XPath("//table[@class='SpecTable']//tr//td"));
这将使您将该表中的所有单元格作为数组,然后可以用于迭代访问文本。
string forOutput = Cells[i].Text;
CSharp方法提取二维数组中的任何表:
private string[,] getYourSpecTable(){
return getArrayBy(By.CssSelector("table.SpecTable tr"), By.CssSelector("td"));
}
private string[,] getArrayBy(By rowsBy, By columnsBy){
bool init=false;
int nbRow=0, nbCol=0;
string[,] ret = null;
ReadOnlyCollection<OpenQA.Selenium.IWebElement> rows = this.webDriver.FindElements(rowsBy);
nbRow = rows.Count;
for(int r=0;r<nbRow;r++) {
ReadOnlyCollection<OpenQA.Selenium.IWebElement> cols = rows[r].FindElements(columnsBy);
if(!init) {
init= true;
nbCol = cols.Count;
ret = new string[rows.Count, cols.Count];
}
for(int c=0;c<nbCol;c++) {
ret[r, c] = cols[c].Text;
}
}
return ret;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.