2010-02-02 74 views
10

有一個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">}> 
] 

回答

10

我想你要尋找的是

login_form.add_field!(field_name, value = nil) 

這裏是文檔:

http://rdoc.info/projects/tenderlove/mechanize

這和WWW :: Mechanize :: Form :: Field.new方法之間的區別並不多,除了沒有很多方法將字段添加到表單之外。以下是add_field的用法!方法實施....你可以看到,這正是你所期望的。它實例化一個Field對象,然後將其添加到窗體的'fields'數組中。您將無法在代碼中執行此操作,因爲「字段< <」是「Form」中的私有方法。

# File lib/www/mechanize/form.rb, line 65 
    def add_field!(field_name, value = nil) 
    fields << Field.new(field_name, value) 
    end 

在一個側面說明,根據文檔,你應該能夠做你提出的第一個變化:

login_form['field_name']='value' 

希望這有助於!

+0

@btelles時間:作品!那麼你的代碼和http://mechanize.rubyforge.org/mechanize/WWW/Mechanize/Form/Field.html公共方法類new有什麼區別? – Radek 2010-02-03 03:57:23

+0

我以爲'login_form ['field_name'] ='value''不起作用,因爲我試圖添加字段而不是修改。也許有人可以詳細說明,如果有更多的知識。 – Radek 2010-02-03 04:32:36

+2

如果您單擊[] =方法的「show source」,您將看到如果通過運行「add_field!」,實現將創建一個字段(如果該字段尚不存在)。方法。它說「這個名字已經存在了嗎?」如果沒有,那麼「add_field!(field_name,value)」... – btelles 2010-02-03 10:08:35

5

另一種方式如何添加新的領域是如此,在發佈形式

page = agent.post(url, {'auth_username'=>'myusername', #existing field 
         'auth_password'=>'mypassword', #existing field 
         'auth_login'=>'Login'}) #new field