1

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

Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53

2 Answers2

0

Just parse the body with nokogiri:

link = page.link_with(:href => %r{/en/users}).click
doc = Nokogiri::HTML link.body
agent.get doc.at('a')[:href]
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

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
zhon
  • 1,610
  • 1
  • 22
  • 31