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>定义表格列的组。
  • 相关阅读:
    后缀数组板子
    上海高校金马五校赛 J
    西安电子科技大学第16届程序设计竞赛网络同步赛 G-小国的复仇
    HDU
    string 与char* char[]之间的转换 .
    (分治思想)(归并排序)C
    如何取出 Map中key和value的值
    C++ STL 中 map 容器的说明和使用技巧 .
    (经典map)A
    Babelfish(6.1.2)(sort结构体排序)(sscanf()基本使用方法)(二分法)
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4329826.html
Copyright © 2011-2022 走看看