1. 程式人生 > >selenium 如何處理table

selenium 如何處理table

以前在selenium RC 裡面有一個getTable方法,是得到一個單元格中的文字。其詳細描述如下:

Java程式碼  收藏程式碼
  1. /** Gets the text from a cell of a table. The cellAddress syntax <span style="white-space: normal; background-color: #ffffff;">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: #ffffff;">"foo.1.4"</span>
     
  4. @return the text from the specified cell 
  5. */  
  6. String getTable(String tableCellAddress);  

就是傳入一個引數,這個引數的格式必須是tableLocator.row.column,如"foo.1.4",foo用於得到table物件,1.4代表在table裡第1行第4列。行、列從0開始。

在selenium webdriver裡,沒有這樣的方法,也就是說沒有專門操作table的類。但我們可以自己封閉一個,這並不難。以上面的getTable方法為例,我們自己也可以建立這樣功能的一個方法。

Java程式碼  
收藏程式碼
  1. public String getCellText(By by,String tableCellAddress)  

 我叫它getCellText,它有兩個引數,第一個是By物件用於得到table物件, tableCellAddress 如"1.4",代表在table裡第1行第4列。行、列從0開始。

以下面html程式碼為例:

Html程式碼  收藏程式碼
  1. <html>  
  2.     <head>  
  3.         <title>Table</title>  
  4.     </head>  
  5.     <body>  
  6.         <table border="1" id="myTable">  
  7.             <tr>  
  8.                 <th>Heading(row 0 ,cell 0)</th>  
  9.                 <th>Another Heading(row 0 ,cell 1)</th>  
  10.                 <th>Another Heading(row 0 ,cell 2)</th>  
  11.             </tr>  
  12.             <tr>  
  13.                 <td>row 1, cell 0</td>  
  14.                 <td>row 1, cell 1</td>  
  15.                 <td>row 1, cell 2</td>  
  16.             </tr>  
  17.             <tr>  
  18.                 <td>row 2, cell 0</td>  
  19.                 <td>row 2, cell 1</td>  
  20.                 <td>row 2, cell 2</td>  
  21.             </tr>  
  22.         </table>  
  23.     </body>  
  24. </html>  

示例程式碼如下:

Java程式碼  收藏程式碼
  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. public class Table {  
  8.     /** 
  9.      * @author gongjf 
  10.      */  
  11.     private WebDriver driver;  
  12.     Table(WebDriver driver){  
  13.         this.driver = driver;  
  14.     }  
  15.     /** 從一個table的單元格中得到文字值. 引數tableCellAddress的格式為 
  16.     row.column, 行列從0開始. 
  17.     @param by  用於得到table物件 
  18.     @param tableCellAddress 一個單元格地址, 如. "1.4" 
  19.     @return 從一個table的單元格中得到文字值 
  20.     */  
  21.     public String getCellText(By by,String tableCellAddress) {  
  22.         //得到table元素物件  
  23.         WebElement table = driver.findElement(by);  
  24.         //對所要查詢的單元格位置字串進行分解,得到其對應行、列。  
  25.         int index = tableCellAddress.trim().indexOf('.');  
  26.         int row =  Integer.parseInt(tableCellAddress.substring(0, index));  
  27.         int cell = Integer.parseInt(tableCellAddress.substring(index+1));  
  28.         //得到table表中所有行物件,並得到所要查詢的行物件。  
  29.          List<WebElement> rows = table.findElements(By.tagName("tr"));  
  30.          WebElement theRow = rows.get(row);  
  31.          //呼叫getCell方法得到對應的列物件,然後得到要查詢的文字。  
  32.          String text = getCell(theRow, cell).getText();  
  33.          return text;  
  34.     }  
  35.     private WebElement getCell(WebElement Row,int cell){  
  36.          List<WebElement> cells;  
  37.          WebElement target = null;  
  38.          //列裡面有"<th>"、"<td>"兩種標籤,所以分開處理。  
  39.          if(Row.findElements(By.tagName("th")).size()>0){  
  40.             cells = Row.findElements(By.tagName("th"));  
  41.             target = cells.get(cell);  
  42.          }  
  43.          if(Row.findElements(By.tagName("td")).size()>0){  
  44.             cells = Row.findElements(By.tagName("td"));  
  45.             target = cells.get(cell);  
  46.          }  
  47.         return target;  
  48.     }  
  49.     public static void main(String[] args) {  
  50.          WebDriver driver;  
  51.          System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");    
  52.          driver = new FirefoxDriver();  
  53.          driver.get("file:///C:/Documents and Settings/Gongjf/桌面/selenium_test/table.html");  
  54.          Table table = new Table(driver);  
  55.          By by = By.id("myTable");  
  56.          String address = "0.2";  
  57.          System.out.println(table.getCellText(by, address));  
  58.     }  
  59. }  

 執行程式碼將輸出

Java程式碼  收藏程式碼
  1. Another Heading(row 0 ,cell 2)