如何向机械化表单添加新字段(ruby/mechanize) [英] how to add new field to mechanize form (ruby/mechanize)

查看:34
本文介绍了如何向机械化表单添加新字段(ruby/mechanize)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个公共类方法来添加字段机械化形式

there is a public class method to add field to mechanize form

我试过了..

#login_form.field.new('auth_login','Login')
#login_form.field.new('auth_login','Login')

并且两者都给我一个错误 undefined method "new" for #<WWW::Mechanize::Form::Field:0x3683cbc>(NoMethodError)

and both gives me an error undefined method "new" for #<WWW::Mechanize::Form::Field:0x3683cbc> (NoMethodError)

我尝试了 login_form.field.new('auth_login','Login') 这给了我一个错误

I tried login_form.field.new('auth_login','Login') which gives me an error

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 这可能是我无法登录的原因.只是我的想法.

but at the time I submit the form. The field does not exist in html source. I want to add it so POST query sent by my script will contain auth_username=myusername&auth_password=mypassword&auth_login=Login So far it sends only auth_username=radek&auth_password=mypassword which might be why I cannot get logged in. Just my thought.

脚本看起来像

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 看起来像

The .inspect of the form looks like

[#<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">}>
]

推荐答案

我想你要找的是

login_form.add_field!(field_name, value = nil)

这里是文档:

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

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

The difference between this and the method WWW::Mechanize::Form::Field.new is not much, aside from the fact that there aren't many ways to add fields to a form. Here's how the add_field! method is implemented....you can see that it's exactly what you'd expect. It instantiates a Field object, then adds it to the form's 'fields' array. You wouldn't be able to do this in your code because the method "fields<<" is a private method inside "Form."

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

顺便说一句,根据文档,您应该能够执行您提出的第一个变体:

On a side note, according to the docs you should be able to do the first variation you proposed:

login_form['field_name']='value'

希望这有帮助!

这篇关于如何向机械化表单添加新字段(ruby/mechanize)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆