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>  
    <head>  
        <title>Table</title>  
          
    </head>  
    <body>  
        <table border="1" id="myTable">  
            <tr>  
                <th>Heading(row 0 ,cell 0)</th>  
                <th>Another Heading(row 0 ,cell 1)</th>  
                <th>Another Heading(row 0 ,cell 2)</th>  
            </tr>  
            <tr>  
                <td>row 1, cell 0</td>  
                <td>row 1, cell 1</td>  
                <td>row 1, cell 2</td>  
            </tr>  
            <tr>  
                <td>row 2, cell 0</td>  
                <td>row 2, cell 1</td>  
                <td>row 2, cell 2</td>  
            </tr>  
        </table>  
    </body>  
</html>  


 

示例代码如下:

import java.util.List;  
import org.openqa.selenium.By;  
import org.openqa.selenium.NoSuchElementException;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.firefox.FirefoxDriver;  
  
public class Table {  
  
    /** 
     * @author gongjf 
     */  
    private WebDriver driver;  
    Table(WebDriver driver){  
        this.driver = driver;  
    }  
      
    /** 从一个table的单元格中得到文本值. 参数tableCellAddress的格式为 
    row.column, 行列从0开始. 
    @param by  用于得到table对象 
    @param tableCellAddress 一个单元格地址, 如. "1.4" 
    @return 从一个table的单元格中得到文本值 
    */  
    public String getCellText(By by,String tableCellAddress) {  
        //得到table元素对象  
        WebElement table = driver.findElement(by);  
        //对所要查找的单元格位置字符串进行分解,得到其对应行、列。  
        int index = tableCellAddress.trim().indexOf('.');  
        int row =  Integer.parseInt(tableCellAddress.substring(0, index));  
        int cell = Integer.parseInt(tableCellAddress.substring(index+1));  
        //得到table表中所有行对象,并得到所要查询的行对象。  
         List<WebElement> rows = table.findElements(By.tagName("tr"));  
         WebElement theRow = rows.get(row);  
         //调用getCell方法得到对应的列对象,然后得到要查询的文本。  
         String text = getCell(theRow, cell).getText();  
         return text;  
    }  
    private WebElement getCell(WebElement Row,int cell){  
         List<WebElement> cells;  
         WebElement target = null;  
         //列里面有"<th>"、"<td>"两种标签,所以分开处理。  
         if(Row.findElements(By.tagName("th")).size()>0){  
            cells = Row.findElements(By.tagName("th"));  
            target = cells.get(cell);  
         }  
         if(Row.findElements(By.tagName("td")).size()>0){  
            cells = Row.findElements(By.tagName("td"));  
            target = cells.get(cell);  
         }  
        return target;  
           
    }  
      
      
    public static void main(String[] args) {  
         WebDriver driver;  
         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");    
         driver = new FirefoxDriver();  
         driver.get("file:///C:/Documents and Settings/Gongjf/桌面/selenium_test/table.html");  
      
         Table table = new Table(driver);  
         By by = By.id("myTable");  
         String address = "0.2";  
       
         System.out.println(table.getCellText(by, address));  
          
          
    }  
  
}  


运行代码将输出

 

 

Java代码   收藏代码
  1. Another Heading(row 0 ,cell 2)  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值