(十五)遍历表格
也算(十四)WebDriver.findElement()和WebElement.findElement()之间的区别的一个应用
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkbox</title>
</head>
<body>
<h3>table</h3>
<table width="400" border="1" id="table">
<tr>
<td align="left">消费项目...</th>
<td align="right">一月</th>
<td align="right">二月</th>
</tr>
<tr>
<td align="left">衣服</th>
<td align="right">1000元</th>
<td align="right">500元</th>
</tr>
<tr>
<td align="left">化妆品</th>
<td align="right">3000元</th>
<td align="right">400元</th>
</tr>
<tr>
<td align="left">总计</th>
<td align="right">4000元</th>
<td align="right">900元</th>
</tr>
</body>
</html>
@Test
public void testUntitledTestCase() throws Exception {
driver.get("file:///C:/Users/a/Desktop/select.html");
WebElement div=driver.findElement(By.id("table"));
List<WebElement> rows=div.findElements(By.tagName("tr"));
System.out.println(rows.size());
for(WebElement row:rows)
{
List<WebElement> cols=row.findElements(By.tagName("td"));
for(WebElement col:cols)
{
System.out.print(col.getText());//获取标签文本
}
System.out.println();
}
}