to fetch all values of a specific column in webtable in webdriver

8,527 views
Skip to first unread message

sachin poovaiah

unread,
Mar 26, 2013, 5:04:52 AM3/26/13
to seleniu...@googlegroups.com
Column1Column2Column3Column4Column5Column6Column7
hi
I have a search criteria which i have to automate. the search result grid luks lik the above table and to verify the search functionality i need to assert the value present in the column 3rd of all the rows.. At present i have implemented a code where all the values from the table is being read and stored in a list. but i need only the third column value. please help.
my code is
 
 List<WebElement> tr_collection = driver.findElements(By.xpath(".//table[@id='fsConnectorRow']/tbody/tr"));
  System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
  int row_num=1,col_num=1;
  for (WebElement trElement : tr_collection)
  {
    List<WebElement> td_collection = trElement.findElements(By.tagName("td")); 
   int n = td_collection.size();
   for(int i=0;i<td_collection.size();i++)
    System.out.println(td_collection.get(i).getText());
   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++;
  }
 
 
 
 
I need only 3rd column value please help

Kevin O.

unread,
Mar 26, 2013, 10:29:57 AM3/26/13
to seleniu...@googlegroups.com
You can just adjust your XPath to select only the 3rd td element.
 
 List<WebElement> column3 = driver.findElements(By.xpath(".//table[@id='fsConnectorRow']/tbody/tr/td[3]"));
  System.out.println("NUMBER OF ROWS IN THIS TABLE = "+ column3 .size());
  int row_num=1;
  for (WebElement tdElement : column3 )
  {
        System.out.println("row # " + row_num + ", col #3 text=" +tdElement.getText());
   row_num++;
  }
 

sachin poovaiah

unread,
Mar 26, 2013, 1:49:01 PM3/26/13
to seleniu...@googlegroups.com
@ kevin

Thanks a ton :)  
the code worked fine :) 

dhanesh jadhav

unread,
Jan 10, 2019, 5:58:15 AM1/10/19
to Selenium Users
What if I only want the data of column 2 and 4 simultaneously?

kiran kumar

unread,
Jan 11, 2019, 4:44:09 AM1/11/19
to Selenium Users
Sachin's and Kevin'd reply are perfect!

kiran kumar

unread,
Jan 11, 2019, 4:47:39 AM1/11/19
to Selenium Users
While iterating capture the val of  of variable at iteration 3 .if condition(int colnumer = 3)  get the clolumn[3] = int Col3;

kiran kumar

unread,
Jan 11, 2019, 4:48:55 AM1/11/19
to Selenium Users
or you construct x path
Reply all
Reply to author
Forward
0 new messages