Hi, I need to fill out a multi-page form using Mechanize and am trying to figure out how to step through the pages to get back the final results. I''ve tried a few different variations, but can''t get any of them to work. This is the most recent variation. The first submit works well, but then I am unable to figure out how to access and submit the form on the following page. Any help is greatly appreciated!!!! agent.get(data) do |data| data.form(''Page1'') do |data_type| data_type.field("A").option_with(:value => ''1'').select # pp data_type # this verifies that the select option is selected end.submit # ??? The previous submit presents me with another form and I need to select more variables and hit submit again. end Thanks, Cindy -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101222/2732eb2e/attachment.html>
Cindy, You should be able to pass the returned page from the form into a variable and manipulate that: agent.get(data) do |data| next_form = data.form(''Page1'') do |data_type| data_type.field("A").option_with(:value => ''1'').select end.submit next_form.form(''Page2'') ... end -- Benjamin Manns benmanns at gmail.com (434) 321-8324 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101223/39ea59a3/attachment.html>
Hi, thanks so much for your reply. I did something like that at one point in my experimentation, but got stuck on how to submit the next form? This is where it broke down for me. And do I need to do another call to agent.get with subsequent pages? Thanks! Cindy On Dec 22, 2010 11:56 PM, "Benjamin Manns" <benmanns at gmail.com> wrote:> Cindy, > > You should be able to pass the returned page from the form into a variable > and manipulate that: > > > agent.get(data) do |data| > > next_form = data.form(''Page1'') do |data_type| > > data_type.field("A").option_with(:value => ''1'').select > end.submit > > next_form.form(''Page2'') ... > end > > -- > Benjamin Manns > benmanns at gmail.com > (434) 321-8324-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101223/ee10b2fa/attachment.html>
Cindy, Ah, to chain them all together: agent.get(data) do |data| next_form = data.form(''Page1'') do |data_type| data_type.field("A").option_with(:value => ''1'').select end.submit third_form = next_form.form(''Page2'') do |data_type| data_type.field("B").option_with(:value => ''2'').select end.submit fourth_form = third_form.form(''Page3'') do |data_type| data_type.field("C").option_with(:value => ''3'').select end.submit final_page = fourth_form.form(''Page4'') do |data_type| data_type.field("D").option_with(:value => ''4'').select end.submit # from here on you can use final_page.methods (links, images, forms, ...) end Each call to submit, get, etc. returns a Mechanize::Page, which comes with all kinds of cool methods. You can read the documentation on all of them here: http://mechanize.rubyforge.org/mechanize/Mechanize/Page.html. Ben -- Benjamin Manns benmanns at gmail.com (434) 321-8324 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101223/650a0f5a/attachment.html>
Thanks for your help. I was really close. It''s working great now. Cindy From: mechanize-users-bounces at rubyforge.org [mailto:mechanize-users-bounces at rubyforge.org] On Behalf Of Benjamin Manns Sent: Thursday, December 23, 2010 11:58 AM To: Ruby Mechanize Users List Subject: Re: [Mechanize-users] Multi-Step forms with Mechanize Cindy, Ah, to chain them all together: agent.get(data) do |data| next_form = data.form(''Page1'') do |data_type| data_type.field("A").option_with(:value => ''1'').select end.submit third_form = next_form.form(''Page2'') do |data_type| data_type.field("B").option_with(:value => ''2'').select end.submit fourth_form = third_form.form(''Page3'') do |data_type| data_type.field("C").option_with(:value => ''3'').select end.submit final_page = fourth_form.form(''Page4'') do |data_type| data_type.field("D").option_with(:value => ''4'').select end.submit # from here on you can use final_page.methods (links, images, forms, ...) end Each call to submit, get, etc. returns a Mechanize::Page, which comes with all kinds of cool methods. You can read the documentation on all of them here: http://mechanize.rubyforge.org/mechanize/Mechanize/Page.html. Ben -- Benjamin Manns benmanns at gmail.com (434) 321-8324 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101223/e1913698/attachment-0001.html>