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

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

问题描述

有一个公共类方法可以将字段添加到机械化表格

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)

我尝试了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"数组中.您将无法在代码中执行此操作,因为方法"fields<<"是表单"中的私有方法.

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'

希望这会有所帮助!

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

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