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

后端 未结 2 1546
感情败类
感情败类 2021-02-20 10:28

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

I tried ..

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


        
2条回答
  •  一个人的身影
    2021-02-20 10:52

    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!

提交回复
热议问题