WebDriver获得表格里所有单元格的文本

发表于:2015-2-13 11:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:open经验库    来源:51Testing软件测试网论坛

  方法为:
  1. 得到表格中所有的tr,存到list到中
  2.对tr进行循环,根据当前的tr,得到当前所有td的集合存到list当中
  3.循环中所有td里的文本
  具体实现为
package com.example.tests;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Selenium2 {
WebDriver driver = new InternetExplorerDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
@Test
public void tableTest() {
driver.get("http://www.w3school.com.cn/html/html_tables.asp");
//首先得到所有tr的集合
List<WebElement> rows = driver.findElements(By.cssSelector(".dataintable tr"));
//验证表格的行数
assertEquals(11,rows.size());
//打印出所有单元格的数据
for (WebElement row : rows) {
//得到当前tr里td的集合
List<WebElement> cols =  row.findElements(By.tagName("td"));
for (WebElement col : cols) {
System.out.print(col.getText());//得到td里的文本
}
System.out.println();
}
driver.close();
}
}
  打印结果为
  ---------------------------------------------------------------------------------
  <table>定义表格
  <caption>定义表格标题。
  <th>定义表格的表头。
  <tr>定义表格的行。
  <td>定义表格单元。
  <thead>定义表格的页眉。
  <tbody>定义表格的主体。
  <tfoot>定义表格的页脚。
  <col>定义用于表格列的属性。
  <colgroup>定义表格列的组。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号