I have a table:
html ='
<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 style="background-color: #D8E4F6;vertical-align: top;">
<td nowrap="nowrap"><b>Bill Date</b></td>
<td nowrap="nowrap"><b>Bill Amount</b></td>
<td nowrap="nowrap"><b>Bill Due Date</b></td>
<td nowrap="nowrap"><b>Bill (PDF)</b></td>
</tr>
</table>
'
I use the codes suggested in this post (XPath matching text in a table - Ruby - Nokigiri). It works fine if I use any words in the first row as the match word, for example "Statement". But it doesn't work if I use words that in the other row, for example "Amount".
doc = Nokogiri::HTML("#{html}")
doc.xpath('//table[contains(descendant::*, "Statement")]').each do |node|
puts node.text
end
The contains()
function expects a singular value as its first argument. descendant::*
may yield multiple elements then causing the function to behave unexpectedly, such as considering only the first element yielded.
Try to change your XPath to be:
doc.xpath('//table[descendant::*[contains(., "Amount")]]').each do |node|
puts node.text
end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments