如何处理表行和列

时间:2016-08-29 15:29:53

标签: java selenium selenium-webdriver selenium-chromedriver

我正在使用Java和Selenium编写测试。在我的目标应用程序DOM中,我需要经历许多表。我曾经使用过:

  WebElement mytable = driver.findElement(By.xpath(".//table/tbody"));
  List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
  int rows_count = rows_table.size();
  for (int row=0; row<rows_count; row++){
   List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
   int columns_count = Columns_row.size();
   System.out.println("Number of cells In Row "+row+" are "+columns_count);
   for (int column=0; column<columns_count; column++){
    String celtext = Columns_row.get(column).getText();
    System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
   }
   System.out.println("--------------------------------------------------");
  }  

但是我正在寻找能够以更简单的方式处理表格的东西,所以我搜索并发现:

1)selenium.getTable

2)GetTable(ElementFinder finder, JavascriptLibrary js)

但是我找不到任何好的代码示例。

长话短说我想知道是否有更好的方法而不是找到.//tr.//td来处理表格中的行和列?

2 个答案:

答案 0 :(得分:1)

getTable未在webdriver中实现。但你可以试试这个。

WebDriver driver = new ChromeDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "your website url");
selenium .getTable("xpath");

selenium.getTable(&#34; table.1.2&#34);     getTable(tableCellAddress)参数:

tableCellAddress - a cell address, e.g. "foo.1.4"

Returns: the text from the specified cell

Gets the text from a cell of a table. The cellAddress syntax tableLocator.row.column, where row and column start at 0.

为方法添加一个抑制警告。

答案 1 :(得分:0)

类似问题的答案在以下主题中回答:

Extracting text from webtable selenium webdriver

HashMap<Integer, HashMap<String, String>> tableData = new HashMap<Integer,     HashMap<String, String>>();
  HashMap<String, String> tableCellDataMap = null;
  WebElement table = driver.findElement(By.id("<table_id>"));

  /**
   *  Call this method to get Table Cell Data by passing WebElement table
   *  @Params table
   *  
   *  @author Fayaz
   **/
  public static HashMap<String, String> getTableData(WebElement table) {
  List<WebElements> tableColHeaders = table.findElements(By.tagName("th"));
  List<WebElements> tableRows = table.findElements(By.tagName("tr"));

  // Start rowIndex with '1' because in the zeroth index we get 'th' tags   
  for(int rowIndex = 1; rowIndex < tableRows.size(); rowIndex++) {
     List<WebElement> tableCells = tableRows.get(rowIndex).findElements(By.tagName("td"));
     for(int cellIndex = 0; cellIndex < tableCells.size(); cellIndex++) {
         String tableCellData = tableCells.get(cellIndex).getText();
         String tableColName  = tableColHeaders.get(cellIndex).getText();
         tableCellDataMap     = new HashMap<String, String>();
         tableCellDataMap.put(tableColName, tableCellData);
     }
     tableData.put(rowIndex, tableCellDataMap);
  }
  return tableCellDataMap;
 }

实际上,甚至可以进一步模块化使用getTableHeader(),getRowSize(),getColSize(),getData(int rowIndex),getData(int colIndex)等方法制作Table实用程序类

如果我们有多个表,那么也循环表