hay una public class method añadir campo para mecanizar la formacómo agregar nuevo campo de mecanizar forma (rubí/mecanizar)
Probé ..
#login_form.field.new('auth_login','Login')
#login_form.field.new('auth_login','Login')
y ambos me da un error undefined method "new" for #<WWW::Mechanize::Form::Field:0x3683cbc> (NoMethodError)
me trataron login_form.field.new('auth_login','Login')
que me da un error
mechanize-0.9.3/lib/www/mechanize/page.rb:13 n `meta': undefined method `search' for nil:NilClass (NoMethodError)
pero en el momento en que presento el formulario. El campo no existe en la fuente html. Quiero agregarlo para que la consulta POST enviada por mi secuencia de comandos contenga auth_username=myusername&auth_password=mypassword&auth_login=Login
Hasta ahora envía solo auth_username=radek&auth_password=mypassword
que podría ser el motivo por el que no puedo iniciar sesión. Solo mi pensamiento.
El guión parece
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) }
El .inspect de la forma se parece a
[#<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">}>
]
@btelles: obras! Entonces, ¿cuál es la diferencia entre su código y http://mechanize.rubyforge.org/mechanize/WWW/Mechanize/Form/Field.html clase de método público nuevo para el campo? – Radek
Pensé que 'login_form ['field_name'] = 'value'' no funciona porque estaba tratando de agregar el campo y no modificarlo. Tal vez alguien pueda dar más detalles sobre eso si uno tiene más conocimiento sobre eso. – Radek
Si hace clic en "mostrar origen" para el método [] =, verá que la implementación crea un campo si aún no existe ejecutando el "add_field!" método. Dice "¿ya existe un campo con ese nombre?" si no, entonces "add_field! (field_name, value)" ... – btelles