如何使用WebDriver获得表格里所有单元格的文本-自动化测试
更新时间:2022-05-05 09:24:38 作者:多测师 浏览:195
方法为:
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>定义表格列的组。
以上内容为大家介绍了自动化测试中的如何使用WebDriver获得表格里所有单元格的文本,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/