Jonathan Huizingh
2009-Jun-18 02:55 UTC
[Mechanize-users] Migrate from Hpricot to Nokogiri
Hi, I was using Mechanize with Hpricot in a rails app about 6 months ago. I recently started to work on that app again, and was getting an error: "undefined method `text'' for #<Array:0x7f8b0d349be0>". It took me a little bit of research, but I figured out that I was using a newer version of the Mechanize gem on this machine and that it had switched to using Nokogiri as its parser. I would like to update my code to use Nokogiri since it is now the default. I have been looking around the documentation, but haven''t been able to find any examples of what I am trying to do. Can somebody give me an example of how I would update the following line? unless logged_in_page.links.text(/link text to find/).length > 0 ... more code here ... end I also saw that there were Hpricot decorators for Nokogiri objects. I assume this would allow me to use my Hpricot code as-is, but I couldn''t find an example of how to do that. Could somebody give me an example of that? Thanks, Jonathan
On Wed, Jun 17, 2009 at 7:55 PM, Jonathan Huizingh<jhuizingh at mindless.com> wrote:> Hi, > > I was using Mechanize with Hpricot in a rails app about 6 months ago. > I recently started to work on that app again, and was getting an > error: ?"undefined method `text'' for #<Array:0x7f8b0d349be0>". ?It > took me a little bit of research, but I figured out that I was using a > newer version of the Mechanize gem on this machine and that it had > switched to using Nokogiri as its parser. >Yes! Your problem isn''t to do with hpricot vs nokogiri, but that the links api has changed. "links" returns a normal array now, so you can''t call "text" on it.> I would like to update my code to use Nokogiri since it is now the > default. ?I have been looking around the documentation, but haven''t > been able to find any examples of what I am trying to do. ?Can > somebody give me an example of how I would update the following line? > > unless logged_in_page.links.text(/link text to find/).length > 0 > ? ... more code here ... > endtry this: unless logged_in_page.links_with(:text => /link text to find/).length > 0 ... end> I also saw that there were Hpricot decorators for Nokogiri objects. ?I > assume this would allow me to use my Hpricot code as-is, but I > couldn''t find an example of how to do that. ?Could somebody give me an > example of that?If you''re only calling methods on mechanize, you shouldn''t need to do that. -- Aaron Patterson http://tenderlovemaking.com/
Jonathan Huizingh
2009-Jun-21 20:51 UTC
[Mechanize-users] Migrate from Hpricot to Nokogiri
Thanks. Your suggestion worked like a charm. I think what confused me is that this page (http://mechanize.rubyforge.org/mechanize/EXAMPLES_rdoc.html) has examples that show my way of doing it, even though they don''t seem to work anymore. Thanks again for the help. Jonathan On Thu, Jun 18, 2009 at 11:47 AM, Aaron Patterson<aaron.patterson at gmail.com> wrote:> > Yes! ?Your problem isn''t to do with hpricot vs nokogiri, but that the > links api has changed. ?"links" returns a normal array now, so you > can''t call "text" on it. > >> I would like to update my code to use Nokogiri since it is now the >> default. ?I have been looking around the documentation, but haven''t >> been able to find any examples of what I am trying to do. ?Can >> somebody give me an example of how I would update the following line? >> >> unless logged_in_page.links.text(/link text to find/).length > 0 >> ? ... more code here ... >> end > > try this: > > ?unless logged_in_page.links_with(:text => /link text to find/).length > 0 > ? ?... > ?end >