Search code examples
rubyrubygemsmechanizemechanize-ruby

is it possible to convert Mechanize::File into Mechanize::Page


I'm having a trouble with Mechanize gem, how to convert Mechanize::File into Mechanize::Page,

here's my piece of code:

**link** = page.link_with(:href => %r{/en/users}).click

when users link clicked it goes to the page with the list of users, now i want to click the first user, but i can't achieve this, because link return Mechanize::File object

Any help, suggestions 'd be great, thanks


Solution

  • Mechanize uses Content-Type to determine how the resource should be handled. Occasionally websites will not set the mime-types for their resources. Mechanize::File is the default for unset Content-Type.

    If you are only dealing with 'text/html' you can following Jimm Stout's suggestion of using post_connect_hooks

    agent = Mechanize.new do |a|
      a.post_connect_hooks << ->(_,_,response,_) do
        if response.content_type.empty?
          response.content_type = 'text/html'
        end
      end
    end