Find a table containing specific text

旧街凉风 提交于 2019-12-11 11:00:09

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/24301569/find-a-table-containing-specific-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!