有一個public class method加場機械化形式如何添加新的領域實現機械化形式(紅寶石/機械化)
我試過..
#login_form.field.new('auth_login','Login')
#login_form.field.new('auth_login','Login')
無一不給我一個錯誤undefined method "new" for #<WWW::Mechanize::Form::Field:0x3683cbc> (NoMethodError)
我試圖login_form.field.new('auth_login','Login')
這給了我一個錯誤
mechanize-0.9.3/lib/www/mechanize/page.rb:13 n `meta': undefined method `search' for nil:NilClass (NoMethodError)
但我在提交表格時。該字段不存在於html源代碼中。我想添加它,所以我的腳本發送POST查詢將包含auth_username=myusername&auth_password=mypassword&auth_login=Login
到目前爲止,它只發送auth_username=radek&auth_password=mypassword
這可能是爲什麼我不能登錄。只是我的想法。
腳本看起來像
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')
puts login_form.buttons.inspect
puts page.forms.inspect
#STDIN.gets
login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }
login_form['auth_username'] = 'radeks'
login_form['auth_password'] = 'TestPass01'
#login_form['auth_login'] = 'Login'
#login_form.field.new('auth_login','Login')
#login_form.field.new('auth_login','Login')
#login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }
#STDIN.gets
page = agent.submit login_form
#Display welcome 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) }
形式的.inspect看起來像
[#<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">}>
]
@btelles時間:作品!那麼你的代碼和http://mechanize.rubyforge.org/mechanize/WWW/Mechanize/Form/Field.html公共方法類new有什麼區別? – Radek 2010-02-03 03:57:23
我以爲'login_form ['field_name'] ='value''不起作用,因爲我試圖添加字段而不是修改。也許有人可以詳細說明,如果有更多的知識。 – Radek 2010-02-03 04:32:36
如果您單擊[] =方法的「show source」,您將看到如果通過運行「add_field!」,實現將創建一個字段(如果該字段尚不存在)。方法。它說「這個名字已經存在了嗎?」如果沒有,那麼「add_field!(field_name,value)」... – btelles 2010-02-03 10:08:35