2011-06-01 108 views
23

在Selenium 2.0中,我不知道如何遍历网页中的HTML表格。在selenium2.0 javadoc中,我找到了两个类“TableFinder”和“TableCellFinder”,但我找不到任何示例。如何从HTML表格的每个单元格获取文本?

我想要做这样的事情:

RowCount=Get how many rows are there in the html table 

for each row of the table 
{ 
    column_count=Get column count 
    for each column 
    { 
     cell_value=get_text_from(row,col); 
     Do something with cell_value 
    } 
} 

我怎样才能从每个表格单元格中的文本?

回答

43

感谢刚才的答复得到总的行和列。

我想出了使用硒2.0类的解决方案。

import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.ie.InternetExplorerDriver; 

public class WebTableExample 
{ 
    public static void main(String[] args) 
    { 
     WebDriver driver = new InternetExplorerDriver(); 
     driver.get("http://localhost/test/test.html");  

     WebElement table_element = driver.findElement(By.id("testTable")); 
     List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr")); 

     System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size()); 
     int row_num,col_num; 
     row_num=1; 
     for(WebElement trElement : tr_collection) 
     { 
      List<WebElement> td_collection=trElement.findElements(By.xpath("td")); 
      System.out.println("NUMBER OF COLUMNS="+td_collection.size()); 
      col_num=1; 
      for(WebElement tdElement : td_collection) 
      { 
       System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText()); 
       col_num++; 
      } 
      row_num++; 
     } 
    } 
} 
+0

它对我也有效,谢谢! – kiedysktos 2015-08-26 13:46:57

0

我还没有使用Selenium 2. Selenium 1.x有selenium.getTable("tablename".columnNumber.rowNumber)到达所需的单元格。可能你可以使用webdriverbackedselenium并执行此操作。

而且你可以通过使用

int numOfRows = selenium.getXpathCount("//table[@id='tableid']//tr")

int numOfCols=selenium.getXpathCount("//table[@id='tableid']//tr//td") 
-2

selenium.getTable("tablename".rowNumber.colNumber)

selenium.getTable("tablename".colNumber.rowNumber)

+1

您应该评论此内容,而不是将其发布为答案。 – alestanis 2012-10-20 12:15:31

0
$content = ''; 
    for($rowth=0; $rowth<=100; $rowth++){ 
     $content .= $selenium->getTable("tblReports.{$rowth}.0") . "\n"; 
     //$content .= $selenium->getTable("tblReports.{$rowth}.1") . "\n"; 
     $content .= $selenium->getTable("tblReports.{$rowth}.2") . " "; 
     $content .= $selenium->getTable("tblReports.{$rowth}.3") . " "; 
     $content .= $selenium->getTable("tblReports.{$rowth}.4") . " "; 
     $content .= $selenium->getTable("tblReports.{$rowth}.5") . " "; 
     $content .= $selenium->getTable("tblReports.{$rowth}.6") . "\n"; 

    } 
8

这是我刚煮好了一个C#示例,用希望的CSS选择器,松散的基础上的答案用于看到如何设置表格行的并至少在MS域重复它。我期待通过表行的集合,以找到一个OriginatorsRef(只是一个字符串),并包含与Overdue by title属性在它的图像TD行:

public ReadOnlyCollection<IWebElement> GetTableRows() 
    { 
     this.iwebElement = GetElement(); 
     return this.iwebElement.FindElements(By.CssSelector("tbody tr")); 
    } 

我的主代码中:

 ... 
     ReadOnlyCollection<IWebElement> TableRows; 
     TableRows = f.Grid_Fault.GetTableRows(); 

     foreach (IWebElement row in TableRows) 
     { 
      if (row.Text.Contains(CustomTestContext.Current.OriginatorsRef) && 
       row.FindElements(By.CssSelector("td img[title*='Overdue by']")).Count > 0) 
       return true; 
     } 
0

另一个C#示例。我只是为它做了一个扩展方法。

public static string GetCellFromTable(this IWebElement table, int rowIndex, int columnIndex) 
    { 
     return table.FindElements(By.XPath("./tbody/tr"))[rowIndex].FindElements(By.XPath("./td"))[columnIndex].Text; 
    }