zoukankan      html  css  js  c++  java
  • WebDriver获得表格里所有单元格的文本

    方法为:
      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>定义表格列的组。
  • 相关阅读:
    安卓自己定义View进阶-Canvas之绘制基本形状
    IOS UIPickView+sqlite 选择中国全部城市案例
    linux的主分区与逻辑分区的关系
    Qt:解析命令行
    Firefox默认英文修改中文
    autofs自动挂载
    telent对端口检测状态分析
    WinRAR5.4
    Centos6.5入侵清理
    Win10激活KMS
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4329826.html
Copyright © 2011-2022 走看看