简体   繁体   中英

XPATH - Ruby - Nokogiri - Nodeset

I have a NodeSet of a table that looks similar to this:

<table cellpadding="1" cellspacing="0" width="100%" border="0">
  <tr>
      <td colspan="9" class="csoGreen"><b class="white">Bill Statement Detail</b></td>
  </tr>
  <tr>
      <td><b>Bill Date</b></td>
      <td"><b>Bill Amount</b></td>

      <td"><b>Bill Due Date</b></td>
      <td"><b>Bill (PDF)</b></td>
  </tr>

<tr vAlign="top">
  <td>blahA</td>
  <td>blahB</td>
  <td>blahC</td>

  <td><a href="javascript: void(0);" onclick="javascript:window.open('/cso/displaypdfbill?selectedBillkey=447403730','_blank');">View Bill</a></td>
</tr>

Now I plan on looping through each onclick in the table.

I've been attempting to loop through the NodeSet unsuccessfully.

I ended up with many failed attempts, but I imagine it would end up looking something like this:

doc_list.each_element ("//a[td/text()='onclick']/@href") do |  |
      #here I want to scan and save BlahA into a Variable 
end

You want to iterate through everything with an onclick? Maybe:

doc.css('*[onclick]').each do |el|
    puts el[:onclick]
end

Edit: what you probably really want is the first td of every row starting with the row 3. in that case:

table.css('td[1]')[2..-1].each do |td|
    puts td.text
end

The key to doing this efficiently is not in your question, but in your comment "I want to extract the first td in the tr where there is an onclick".

This expression does exactly that:

doc.xpath('//tr[td/a/@onclick]/td[1]/text()')

In fact this will give you the set of all such matches. No iteration needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM