Search code examples
rubynokogiriopen-uri

Nokogiri stopped searching for sub tags


I was writing my ruby (2.0) script which reads a web page & it was working fine. Then I installed ruby 2.2 and suddenly my nokogiri stopped searching sub tags but not all. It still finds some sub tag and some aren't just being found in the same script. I reverted back to 1.9 but its still doing the same thing. Currently I have installed ruby 2.1.6. I am loading pages with Watir-webdriver and open-uri. The following page is being opened by open-uri.

For an example, here is my code:

htmlPage = '<html>
              <head></head>
              <body>
                <table width="100%" border="1">
                  <tbody>
                    <tr valign="top">
                      <td width="38%" bgcolor="#EFEFEF">
                        <b>
                          <font size="4">NPL Listing History</font>
                        </b>
                      </td>
                      <td width="62%" bgcolor="#EFEFEF">
                        <b>
                          <font size="4">Dates</font>
                        </b>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </body>
            <html>'

page = Nokogiri::HTML(htmlPage)

puts page.css("table [border='1']")

This should get me the table with border 1 but I get nil/null.

Am I doing something wrong? Or something I am missing?

Thanks!


Solution

  • Your problem is the space. In CSS selectors whitespace is the descendant combinator, so table [border='1'] means "all descendants of a table that have a border attribute equal to 1. What you want is table[border='1'], which means "all table elements that have a border attribute equal to 1."