2016-01-30 89 views
0

這有什麼問題?這讓我通過第一個登錄頁面(我認爲),但我似乎仍然註銷。此Ruby Mechanize登錄到stackoverflow腳本有什麼問題?

require 'rubygems' 
require 'mechanize' 

#create agent 
agent = Mechanize.new { |agent| 
    agent.user_agent_alias = 'Mac Safari 4' 
} 
agent.follow_meta_refresh = true 
page = agent.get("https://stackoverflow.com/users/login") 

#get login form 
login_form = page.form_with(:action => "https://stackoverflow.com/users/login") 

login_form.openid_username = "username" 
login_form.openid_identifier = "password" 
page = agent.submit(login_form) 
#pp page 

回答

0
require 'mechanize' 

Mechanize.new do |agent| 
    agent.user_agent_alias = 'Mac Safari' 
    agent.get("https://stackoverflow.com/users/login") 
    agent.page.forms.last do |f| 
    f.email = "username" 
    f.password = "password" 
    f.submit 
    end 
end 

,如果你想用的用戶名/密碼來登錄這個工程。

+0

是嗎? agent.page.uri.to_s == https://stackoverflow.com/users/login這與我從原始代碼得到的結果是一樣的。 – user1222303

+0

您是否有有效的用戶名/密碼登錄進行測試?嘗試使用'agent.get(some_new_uri)'來查看它是否有效。 – sbs