Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - extracting table values

Tags:

ruby

watir

I'm trying to parse a value from an HTML table (below) using ruby, watir and regular expressions. I want to parse the id info from the anchor tag if the table rows have specified values. For example, if Event1, Action2 are my target row selections, then my goal is to get the "edit_#" for the table row.

Table:
Event1 | Action2 
Event2 | Action3 
Event3 | Action4 

Example of HTML (I've cut some info out since this is work code, hopefully you get the idea):

<div id="table1">
  <table class="table1" cellspacing="0">
   <tbody>
      <tr>
      <tr class="normal">
          <td>Event1</td>
          <td>Action2</td>
          <td>
          <a id="edit_3162" blah blah blah… >
          </a>
          </td>
     </tr>
       <tr class="alt">
          <td> Event2</td>
          <td>Action3 </td>
          <td>
          <a id="edit_3163" " blah blah blah…>
          </a>  
          </td>
      </tr>
  </tbody>
</table>
</div>

I have tried the following that doesn't really work:

wb=Watir::Browser.new
wb.goto "myURLtoSomepage"
event = "Event1"
action = "Action2"
table = browser.div(:id, "table1")
policy_row = table.trs(:text, /#{event}#{action/)
puts policy_row
policy_id = policy_row.html.match(/edit_(\d*)/)[1]
puts policy_id

This results in this error which is pointing to the policy_id = ... line : undefined method 'html' for #<Watir::TableRowCollection:0x000000029478f0> (NoMethodError)

Any help is appreciated as I am fairly new to ruby and watir.

like image 577
Dennis Ferguson Avatar asked Jul 09 '26 17:07

Dennis Ferguson


1 Answers

Something like this should work:

browser.table.trs.each do |tr|
  p tr.a.id if tr.td(:index => 0) == "Event1" and tr.td(:index => 1) == "Action2"
end
like image 84
Željko Filipin Avatar answered Jul 11 '26 06:07

Željko Filipin