문제

I am trying to figure out if mechanize sends correct post query.

I want to log in to a forum (please see html source, mechanize log in my other question) but I get only the login page again. When looking into it I can see that firefox sends out post with parameters like

auth_username=myusername&auth_password=mypassword&auth_login=Login but my script sends

auth_username=radek&auth_password=mypassword is that ok or the &auth_login=Login part must be present?

When I tried to add it using login_form['auth_login'] = 'Login' I got an error gems/mechanize-0.9.3/lib/www/mechanize/page.rb:13 inmeta': undefined method search' for nil:NilClass (NoMethodError)

It seems to me that auth_login is a form button not a field (I don't know if it matters)

[#<WWW::Mechanize::Form
 {name nil}
 {method "POST"}
 {action
  "http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1"}
 {fields
  #<WWW::Mechanize::Form::Field:0x36946c0 @name="auth_username", @value="">
  #<WWW::Mechanize::Form::Field:0x369451c @name="auth_password", @value="">}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons
  #<WWW::Mechanize::Form::Button:0x36943b4
   @name="auth_login",
   @value="Login">}>
]

My script is as follow

  require 'rubygems'
  require 'mechanize'
    require 'logger'

  agent = WWW::Mechanize.new {|a| a.log = Logger.new("loginYOTA.log") }

agent.follow_meta_refresh = true #Mechanize does not follow meta refreshes by default, we need to set that option.

page = agent.get("http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1")

login_form = page.form_with(:method => 'POST')  #works

puts login_form.buttons.inspect
puts page.forms.inspect
STDIN.gets

  login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }  
#STDIN.gets
login_form['auth_username'] = 'myusername'
login_form['auth_password'] = 'mypassword'
  login_form['auth_login'] = 'Login'
STDIN.gets  
page = agent.submit login_form
#Display message if logged in

puts page.parser.xpath("/html/body/div/div/div/table/tr/td[2]/div/strong").xpath('text()').to_s.strip
  puts
puts page.parser.xpath("/html/body/div/div/div/table/tr/td[2]/div").xpath('text()').to_s.strip

output = File.open("login.html", "w") {|f| f.write(page.parser.to_html) }

You can find more code, html, log in my other related question log in with browser and then ruby/mechanize takes it over?

도움이 되었습니까?

해결책

the absence of one parameter compare to firefox in POST caused mechanize not to log in. Adding new parameter solved this problem. So it seems to me that the web server requires &auth_login=Login parameter to be in POST.

You can read how to add new field to mechanize form in another question.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top