【问题标题】:how to add new field to mechanize form (ruby/mechanize)如何将新字段添加到机械化表单(红宝石/机械化)
【发布时间】:2011-01-12 01:08:35
【问题描述】:

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

【问题讨论】:

    标签: ruby webforms mechanize


    【解决方案1】:

    我认为你正在寻找的是

    login_form.add_field!(field_name, value = nil)
    

    这里是文档:

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

    这个方法和 WWW::Mechanize::Form::Field.new 方法的区别并不大,除了在表单中添加字段的方法并不多。这是 add_field 的方法!方法已实现....您可以看到这正是您所期望的。它实例化一个 Field 对象,然后将其添加到表单的“字段”数组中。您将无法在代码中执行此操作,因为方法“fields

    # 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'
    

    希望这会有所帮助!

    【讨论】:

    • @btelles:有效!那么你的代码和mechanize.rubyforge.org/mechanize/WWW/Mechanize/Form/Field.html public method class new for field有什么区别?
    • 我认为login_form['field_name']='value' 不起作用,因为我试图添加字段而不是修改。如果对此有更多了解,也许有人可以详细说明。
    • 如果您单击 []= 方法的“显示源代码”,您将看到实现会通过运行“add_field!”创建一个字段,如果该字段尚不存在的话。方法。它说“该名称是否已经存在一个字段?”如果没有,那么 "add_field!(field_name, value)"...
    【解决方案2】:

    另一种添加新字段的方法是在发布表单时添加新字段

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

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 2012-04-28
      • 1970-01-01
      • 2011-01-10
      • 2012-03-03
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多