来源:互联网 发布:淘宝上二手ipad良心店 编辑:程序博客网 时间:2024/04/28 16:33

from http://www.open-open.com/lib/view/open1379225724318.html

方法为:

1. 得到表格中所有的tr,存到list到中

2.对tr进行循环,根据当前的tr,得到当前所有td的集合存到list当中

3.循环中所有td里的文本 

具体实现为

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 =newInternetExplorerDriver();  
    JavascriptExecutor jse = (JavascriptExecutor)driver;  
    @Test  
    publicvoidtableTest() {     
        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>定义表格列的组。



//////////////////////////////////////////

以前在selenium RC 里面有一个getTable方法,是得到一个单元格中的文本。其详细描述如下:

  1. /** Gets the text from a cell of a table. The cellAddress syntax <span style="white-space: normal; background-color: rgb(255, 255, 255);">tableLocator.row.column</span>
  2. , where row and column start at 0.
  3. @param tableCellAddress a cell address, e.g. <span style="white-space: normal; background-color: rgb(255, 255, 255);">"foo.1.4"</span>
  4. @return the text from the specified cell
  5. */ 
  6. String getTable(String tableCellAddress); 
[java] view plaincopyprint?
  1. <span style="font-size:14px;">/** Gets the text from a cell of a table. The cellAddress syntax <span style="white-space: normal; background-color: rgb(255, 255, 255);">tableLocator.row.column</span> 
  2. , where row and column start at 0. 
  3. @param tableCellAddress a cell address, e.g. <span style="white-space: normal; background-color: rgb(255, 255, 255);">"foo.1.4"</span> 
  4. @return the text from the specified cell 
  5. */  
  6. String getTable(String tableCellAddress);</span>  

就是传入一个参数,这个参数的格式必须是tableLocator.row.column,如"foo.1.4",foo用于得到table对象,1.4代表在table里第1行第4列。行、列从0开始。

在selenium webdriver里,没有这样的方法,也就是说没有专门操作table的类。但我们可以自己封闭一个,这并不难。以上面的getTable方法为例,我们自己也可以创建这样功能的一个方法。

  1. public String getCellText(By by,String tableCellAddress) 
[java] view plaincopyprint?
  1. <span style="font-size:14px;">public String getCellText(By by,String tableCellAddress)</span>  

我叫它getCellText,它有两个参数,第一个是By对象用于得到table对象, tableCellAddress 如"1.4",代表在table里第1行第4列。行、列从0开始。

以下面html代码为例:

 

  1. <html> 
  2.     <head> 
  3.         <title>Table</title> 
  4.          
  5.     </head> 
  6.     <body> 
  7.         <tableborder="1"id="myTable"> 
  8.             <tr> 
  9.                 <th>Heading(row 0 ,cell 0)</th> 
  10.                 <th>Another Heading(row 0 ,cell 1)</th> 
  11.                 <th>Another Heading(row 0 ,cell 2)</th> 
  12.             </tr> 
  13.             <tr> 
  14.                 <td>row 1, cell 0</td> 
  15.                 <td>row 1, cell 1</td> 
  16.                 <td>row 1, cell 2</td> 
  17.             </tr> 
  18.             <tr> 
  19.                 <td>row 2, cell 0</td> 
  20.                 <td>row 2, cell 1</td> 
  21.                 <td>row 2, cell 2</td> 
  22.             </tr> 
  23.         </table> 
  24.     </body> 
  25. </html> 
[html] view plaincopyprint?
  1. <span style="font-size:14px;"><html>  
  2.     <head>  
  3.         <title>Table</title>  
  4.           
  5.     </head>  
  6.     <body>  
  7.         <table border="1" id="myTable">  
  8.             <tr>  
  9.                 <th>Heading(row 0 ,cell 0)</th>  
  10.                 <th>Another Heading(row 0 ,cell 1)</th>  
  11.                 <th>Another Heading(row 0 ,cell 2)</th>  
  12.             </tr>  
  13.             <tr>  
  14.                 <td>row 1, cell 0</td>  
  15.                 <td>row 1, cell 1</td>  
  16.                 <td>row 1, cell 2</td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td>row 2, cell 0</td>  
  20.                 <td>row 2, cell 1</td>  
  21.                 <td>row 2, cell 2</td>  
  22.             </tr>  
  23.         </table>  
  24.     </body>  
  25. </html></span>  

示例代码如下:

  1. import java.util.List; 
  2. import org.openqa.selenium.By; 
  3. import org.openqa.selenium.NoSuchElementException; 
  4. import org.openqa.selenium.WebDriver; 
  5. import org.openqa.selenium.WebElement; 
  6. import org.openqa.selenium.firefox.FirefoxDriver; 
  7.  
  8. public class Table { 
  9.  
  10.     /**
  11.      * @author gongjf
  12.      */ 
  13.     private WebDriver driver; 
  14.     Table(WebDriver driver){ 
  15.         this.driver = driver; 
  16.     } 
  17.      
  18.     /** 从一个table的单元格中得到文本值. 参数tableCellAddress的格式为
  19.     row.column, 行列从0开始.
  20.     @param by  用于得到table对象
  21.     @param tableCellAddress 一个单元格地址, 如. "1.4"
  22.     @return 从一个table的单元格中得到文本值
  23.     */ 
  24.     public String getCellText(By by,String tableCellAddress) { 
  25.         //得到table元素对象 
  26.         WebElement table = driver.findElement(by); 
  27.         //对所要查找的单元格位置字符串进行分解,得到其对应行、列。 
  28.         int index = tableCellAddress.trim().indexOf('.'); 
  29.         int row =  Integer.parseInt(tableCellAddress.substring(0, index)); 
  30.         int cell = Integer.parseInt(tableCellAddress.substring(index+1)); 
  31.         //得到table表中所有行对象,并得到所要查询的行对象。 
  32.          List<WebElement> rows = table.findElements(By.tagName("tr")); 
  33.          WebElement theRow = rows.get(row); 
  34.          //调用getCell方法得到对应的列对象,然后得到要查询的文本。 
  35.          String text = getCell(theRow, cell).getText(); 
  36.          return text; 
  37.     } 
  38.     private WebElement getCell(WebElement Row,int cell){ 
  39.          List<WebElement> cells; 
  40.          WebElement target = null
  41.          //列里面有"<th>"、"<td>"两种标签,所以分开处理。 
  42.          if(Row.findElements(By.tagName("th")).size()>0){ 
  43.             cells = Row.findElements(By.tagName("th")); 
  44.             target = cells.get(cell); 
  45.          } 
  46.          if(Row.findElements(By.tagName("td")).size()>0){ 
  47.             cells = Row.findElements(By.tagName("td")); 
  48.             target = cells.get(cell); 
  49.          } 
  50.         return target; 
  51.           
  52.     } 
  53.      
  54.      
  55.     publicstaticvoid main(String[] args) { 
  56.          WebDriver driver; 
  57.          System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  58.          driver = new FirefoxDriver(); 
  59.          driver.get("file:///C:/Documents and Settings/Gongjf/桌面/selenium_test/table.html"); 
  60.      
  61.          Table table = new Table(driver); 
  62.          By by = By.id("myTable"); 
  63.          String address = "0.2"
  64.       
  65.          System.out.println(table.getCellText(by, address)); 
  66.          
  67.          
  68.     } 
  69.  
[java] view plaincopyprint?
  1. <span style="font-size:14px;">import java.util.List;  
  2. import org.openqa.selenium.By;  
  3. import org.openqa.selenium.NoSuchElementException;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.FirefoxDriver;  
  7.   
  8. public class Table {  
  9.   
  10.     /** 
  11.      * @author gongjf 
  12.      */  
  13.     private WebDriver driver;  
  14.     Table(WebDriver driver){  
  15.         this.driver = driver;  
  16.     }  
  17.       
  18.     /** 从一个table的单元格中得到文本值. 参数tableCellAddress的格式为 
  19.     row.column, 行列从0开始. 
  20.     @param by  用于得到table对象 
  21.     @param tableCellAddress 一个单元格地址, 如. "1.4" 
  22.     @return 从一个table的单元格中得到文本值 
  23.     */  
  24.     public String getCellText(By by,String tableCellAddress) {  
  25.         //得到table元素对象  
  26.         WebElement table = driver.findElement(by);  
  27.         //对所要查找的单元格位置字符串进行分解,得到其对应行、列。  
  28.         int index = tableCellAddress.trim().indexOf('.');  
  29.         int row =  Integer.parseInt(tableCellAddress.substring(0, index));  
  30.         int cell = Integer.parseInt(tableCellAddress.substring(index+1));  
  31.         //得到table表中所有行对象,并得到所要查询的行对象。  
  32.          List<WebElement> rows = table.findElements(By.tagName("tr"));  
  33.          WebElement theRow = rows.get(row);  
  34.          //调用getCell方法得到对应的列对象,然后得到要查询的文本。  
  35.          String text = getCell(theRow, cell).getText();  
  36.          return text;  
  37.     }  
  38.     private WebElement getCell(WebElement Row,int cell){  
  39.          List<WebElement> cells;  
  40.          WebElement target = null;  
  41.          //列里面有"<th>"、"<td>"两种标签,所以分开处理。  
  42.          if(Row.findElements(By.tagName("th")).size()>0){  
  43.             cells = Row.findElements(By.tagName("th"));  
  44.             target = cells.get(cell);  
  45.          }  
  46.          if(Row.findElements(By.tagName("td")).size()>0){  
  47.             cells = Row.findElements(By.tagName("td"));  
  48.             target = cells.get(cell);  
  49.          }  
  50.         return target;  
  51.            
  52.     }  
  53.       
  54.       
  55.     public static void main(String[] args) {  
  56.          WebDriver driver;  
  57.          System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");    
  58.          driver = new FirefoxDriver();  
  59.          driver.get("file:///C:/Documents and Settings/Gongjf/桌面/selenium_test/table.html");  
  60.       
  61.          Table table = new Table(driver);  
  62.          By by = By.id("myTable");  
  63.          String address = "0.2";  
  64.        
  65.          System.out.println(table.getCellText(by, address));  
  66.           
  67.           
  68.     }  
  69.   
  70. }  
  71. </span>  

运行代码将输出

 

  1. Another Heading(row 0 ,cell2
[java] view plaincopyprint?
  1. <span style="font-size:14px;">Another Heading(row 0 ,cell 2)</span>  

ps:   这里我只是以得到一个table中单元格的文本为例,但是从代码可以看出,对table的基本操作都有涉及到。有用到的同学可以自己包装一个完整的table类。

http://blog.csdn.net/hanqionglaaa/article/details/8995274

0 0
原创粉丝点击