title: Selenium 2 and html tables
body: <p>Just wondering if there is a better way to get values from a table in selenium 2.  I am currently using 2 for loops I loop over each TR and within each TR I loop over all TD.  so for example if I have a table row with 10 columns I loop 10 times and pull out the text value.  That seems clunky to me.  </p>
  
  <p>My table Rows looks like so </p>
  
  <pre><code>&lt;tr id="cTestData" class="odd"&gt;
  &lt;td class="date_activated"&gt;08/31/2011&lt;/td&gt;
  &lt;td class="date_redeemed"&gt; Not redeemed * &lt;/td&gt;
  &lt;td class="expiration_date"&gt;09/01/2011&lt;/td&gt;
  &lt;td class="product"&gt; State of Maine &lt;/td&gt;
  &lt;td class="value"&gt;$1.00&lt;/td&gt;
  &lt;td class="store"&gt; &amp;ndash; &amp;ndash; &amp;ndash; &lt;/td&gt;
  &lt;td class="offer_details"&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>I think I should be able to say for each table Row get me the TD element with class = date_activated and have it return the date. I tried a few things but nothing seemed to work based on TD class name = foo</p>
  
  <p>If it helps my actual code is</p>
  
  <pre><code>for(WebElement trElement : tr_collection)
          {
              List&lt;WebElement&gt; td_collection=trElement.findElements(By.xpath("td"));
              System.out.println("NUMBER OF COLUMNS="+td_collection.size());
              col_num=1;          
              HashMap actInfo = new HashMap();  // new hashmap for each line inthe result set
  
              if(!td_collection.isEmpty() &amp;&amp; td_collection.size() != 1 ){  
                  for(WebElement tdElement : td_collection)
                  {
                          System.out.println("Node Name=== " + tdElement.getAttribute("class")); 
                          System.out.println("Node Value=== " + tdElement.getText());
                          actInfo.put(tdElement.getAttribute("class"), tdElement.getText());
                      col_num++;
                  }
                  masterMap.add(actInfo);
              } // end if
  
              row_num++;
          }
  </code></pre>