how to add new field to mechanize form (ruby/mechanize)

馋奶兔 提交于 2019-12-04 02:19:43

I think what you're looking for is

login_form.add_field!(field_name, value = nil)

Here are the docs:

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

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'

Hope this helps!

another way how to add new field is to so at the time of posting the form

page = agent.post( url, {'auth_username'=>'myusername',   #existing field
                         'auth_password'=>'mypassword',   #existing field
                         'auth_login'=>'Login'})   #new field
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!