ruby - How to get the index of 'th' element using Nokogiri -


i have following html code , need determine index of "number of strings" using <span> id. i'm using nokogiri parse html , row.

doc = nokogiri::parse(myfile.html) table = doc.xpath("//span[@id='numstrs']/../../..") row = table.xpath["tr[1]"] 

here html:

<tr> <th id ="langframe"> <span id="cabinet"> cabinet</span> </th> <th id ="langbb1"> <span id="bb1"> bb1</span> </th> <th id ="langbb2"> <span id="bb2"> bb2</span> </th> <th id ="langtemp"> <span id="temp"> temperature</span> </th> <th id="langstrs"> <span id="stringspresent"> strings present</span> </th> <th id="langmstrqty"> <span id="numstrs"> number of strings</span> </th> </tr> 

got working, not sure if efficient way it.. works

header = table.xpath("tr[1]") value = header.xpath("//span[@id='#{id}']").text index = header.search('th//text()').collect {|text| text.to_s.strip}.reject(&:empty?).index(value)+1 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -