Getting column values from a web table

366 views
Skip to first unread message

Sridhar Iyer

unread,
Jul 11, 2014, 2:21:00 PM7/11/14
to seleniu...@googlegroups.com
Hello friends,
I want to retrieve the text of each cell in a column in a web table. I get the column index from a method and pass it as a parameter to this method:

01 public List<String> getColumnValues(int colIndex) {
02 WebElement colElement;
03 List<String> colValues = new ArrayList<String>();
04 List<WebElement> rows = getRows();
05 System.out.println("Rows count: " + rows.size());
06 Iterator<WebElement> i = rows.iterator();
07 while (i.hasNext()) {
08 WebElement row = i.next();
09 System.out.println("Row data: " + row.getText());
10 // How to avoid this check for the header row for each row
11 // iteration?
12 if (row.findElements(By.tagName("th")).size() > 0) {
13 colElement = row.findElement(By.xpath(".//th[" + colIndex + "]"));
14 } else {
15 colElement = row.findElement(By.xpath(".//td[" + colIndex + "]"));
16 }
17 colValues.add(colElement.getText().trim());
18 }
19 return colValues;
20 }

Here is my web table:
<table id = "webtable">
<thead>
<tr>
  <th>Header Column 1</th>
  <th>Header Column 2</th>
  <th>Header Column 3</th>
  <th>Header Column 4</th>
  <th>Header Column 5</th>
</tr>
</thead>
<tbody>
<tr> 
<td>Row 1 Col 1</td>
<td>Row 1 Col 2</td>
<td>Row 1 Col 3</td>
<td>Row 1 Col 4</td>
<td>Row 1 Col 5</td>
</tr>
</tbody>
<tbody>
<tr> 
<td>Row 2 Col 1</td>

<td>Row 2 Col 2</td>
<td>Row 2 Col 3</td>
<td>Row 2 Col 4</td>
<td>Row 2 Col 5</td>
</tr>
</tbody>
</table>

The issues:
1. On line 5, I get the proper count of the rows. But, on line 9, it is not picking up the row (cell) values. Its printing blanks. I was expecting Row 1 Col 1, Row 2 Col 2 etc.
2. On line 12, the number of elements with tag "th" returned is only 3, whereas there should be 5.
3. On line 15, I get a no such element exception trying to find .//td[3] even though I can fetch upto //td[5]

Can anybody please explain what I might be doing wrong or if there is a better way to get the column values returned as a List<String>. I need the collection with the column values so that I can use it in my Hamcrest Matcher assertion using the hasitem(item) to check whether my table contains a certain value.

Sridhar Iyer

unread,
Jul 14, 2014, 8:09:45 AM7/14/14
to seleniu...@googlegroups.com
Does anybody have any inputs?

Gokul Krishna

unread,
Jul 14, 2014, 12:29:51 PM7/14/14
to seleniu...@googlegroups.com
row.getText()  doesn't fetch you the cell value. you have to give the column number of the row for which value needs to retrieved.

you can get it by some thing like  row.findElement(By.tagName("td")).getText());

Cheers,
Gokul.


On Mon, Jul 14, 2014 at 5:39 PM, Sridhar Iyer <iyer....@gmail.com> wrote:
Does anybody have any inputs?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/03b1afcd-6cea-4ca9-8e5d-1f1450a110ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robin Gupta

unread,
Jul 15, 2014, 6:19:23 AM7/15/14
to seleniu...@googlegroups.com
You can give this a shot: List<WebElements> lst = driver.findElements(By.xpath("//tr/td"));
Followed by iterator on lst..

Manish Bansal

unread,
Oct 1, 2015, 1:52:03 PM10/1/15
to Selenium Users


On Friday, 11 July 2014 23:51:00 UTC+5:30, Sridhar Iyer wrote:

Shawn Knight

unread,
Oct 2, 2015, 2:04:42 PM10/2/15
to Selenium Users
why not make each row of the table a page-object? A class with predefined methods including one that conducts "matching" so you can search through a list of these page-objects and find the one you are looking for. I have a blog post and example project (in Java) around this concept.



Reply all
Reply to author
Forward
0 new messages