1

I am using Java and Selenium to write a test. In my target application DOM, there are many tables that I need to go through. I used to use things like:

  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("--------------------------------------------------");
  }  

But I was looking for something that can handle tables in an easier way, so I searched and found:

1) selenium.getTable

2) GetTable(ElementFinder finder, JavascriptLibrary js)

but I couldn't find any good code sample for them.

Long story short I was wondering if there is any better way rather than finding .//tr or .//td to handle rows and columns in a table?

2
  • As per documentation you can get the contents of the cell by using getTable(tableLocator.row.column) - where row and column are zero based. Commented Aug 29, 2016 at 15:47
  • @Lsiva thanks could you add it as an answer with all codes and imports? Commented Aug 29, 2016 at 18:54

2 Answers 2

1

getTable is not implemented in webdriver. But you can try this.

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

selenium.getTable("table.1.2"); getTable(tableCellAddress) Arguments:

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.

Add a supress warning for the method.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, where should i get that selenium object from? could you please add descreaption too?
when I use ` DefaultSelenium selenium = ...` there is black line on DefaultSelenium
I just checked it DefaultSelenium is depreciated.
what did you mean by Add a supress warning for the method. ?
because the method is no being deprecated you need to have suppress warning for compiler not complaining about it.
0

Similar Question's answer replied in the following thread:

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;
 }

Actually, it can even be further modularize to make Table utility class with methods like, getTableHeader(), getRowSize(), getColSize(), getData(int rowIndex), getData(int colIndex)

If we have multiple tables, then loop the tables as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.