- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.testng.annotations.Test;
-
- public class LocateTable {
- public WebDriver driver;
- @Test
- public void LocateTableTest() {
- WebElement table = driver.findElement(By.id("table"));
- List<WebElement> rows = table.findElements(By.tagName("tr"));
- // assertEquals(5,rows.size());
- for(WebElement row:rows){
- List<WebElement> cols= row.findElements(By.tagName("td"));
- for(WebElement col:cols){
- System.out.println(col.getText()+"\t");
- }
- System.out.println("");
- }
- }
- }
代码逻辑:
(1)先定位表格的页面元素对象;
WebElement table = driver.findElement(By.id("table"));(2)在表格页面元素对象中,把所有的tr元素对象存储到list对象中(即把表格中每行的对象存储到一个list中)
List<WebElement> rows = table.findElements(By.tagName("tr"));
(3)使用for循环方式,先将表格行对象从rows对象中取出,使用findElements函数将表格行对象中的所有单元格对象存储到名为cols的List对象中,然后再次使用for循环把每行的单元格文本遍历输出。
- List<WebElement> rows = table.findElements(By.tagName("tr"));
- for(WebElement row:rows){
- List<WebElement> cols= row.findElements(By.tagName("td"));
- for(WebElement col:cols){
- System.out.println(col.getText()+"\t");
- }
- System.out.println("");
- }
col.getText()表示获取单元格的文本内容
5373

被折叠的 条评论
为什么被折叠?



