Hi there, Firstly, thanks for Mechanize! I?m finding it incredibly useful so far. The issue I?m stuck on is to do with select lists, I can?t seem to figure out how they should be accessed and selected. Here is the HTML: <form action="" method="POST"> <table id="date-picker-table"> <tr> <td class="top-aligned" rowspan="2"> <select id="date-picker-type-selector" onchange="_AWD_PerformanceModule_setDatePickerType(this.options[this.selectedIndex].value)"> <option value="QUICK" selected>Quick Date Range:</option> <option value="EXACT">Exact Date Range:</option> <option value="COMPARE">Compare Date Ranges:</option> </select> </td> <td id="date-picker-quick-selector"> <select name="interval" onchange="_AWD_PerformanceModule_setSelectedInterval(this.options[this.selectedIndex].value)"> <option value="today" selected>Today</option> <option value="yesterday">Yesterday</option> <option value="last7days">Last 7 days</option> <option value="lastweek">Last week (Mon-Sun)</option> <option value="lastbusinessweek">Last business week (Mon-Fri)</option> <option value="thismonth">This month</option> <option value="lastmonth">Last month</option> <option value="alltime">All time</option> </select> </td> </tr> </table> </form> I?ve been trying to the use the following pieces of Ruby code to change the options in the 2nd list: agent.page.forms[0].set_fields(:interval => "last7days") page = agent.submit(agent.page.forms[0]) And: agent.page.forms[0].interval = ?last7days? page = agent.submit(agent.page.forms[0]) Both of these fail to work as I?m expecting. Can anyone suggest some tips or some examples of how to use correctly use select boxes with Mechanize? Best Regards, Jonathan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20100531/3b6d6e56/attachment.html>
Have you taken a look at the API? http://mechanize.rubyforge.org/mechanize/Mechanize/Form/SelectList.html However, the documentation is not very intuitive. If you want to select a specific option of a dropdown on a webpage, you have to use the argument .select. Let''s say I want to go to JimBeam.com. The landing page has an age verification form. I could use Mechanize to do this for me. So I open up irb, and do the following:> irb > require ''rubygems'' > require ''mechanize'' > agent = Mechanize.new > agent.get("http://www.jimbeam.com/lpa") > form = agent.page.forms.first > year = form.field("year") # Year select list. > month = form.field("month") # Month select list > day = form.field("day") # Day select listOk, now that I have all my variables in place for each of the lists, now I want to tell it to enter my birthdate. Being my birthdate is 3/6/84, I would do the following:> year.option_with(:value => /1984/).select # Changes the selected value to1984> month.option_with(:value => /3/).select # Changes selected value to 3 > day.option_with(:value => /6/).select # Changes selected value to 6Now, I want to submit the form with the new values. So, I''ll do the standard form.submit using my form variable.> form.submitAnd there you have it. So ultimately, you want to find the selectlist to control, then find the option, and then use the .select method to change the value. I hope this helps. Thanks, Justin On Mon, May 31, 2010 at 9:10 AM, <overco at me.com> wrote:> Hi there, > > Firstly, thanks for Mechanize! I?m finding it incredibly useful so far. > > The issue I?m stuck on is to do with select lists, I can?t seem to figure > out how they should be accessed and selected. > > Here is the HTML: > > *<form action="" method="POST">* > * **<table id="date-picker-table">* > * **<tr>* > * **<td class="top-aligned" rowspan="2">* > * **<select id="date-picker-type-selector" > onchange="_AWD_PerformanceModule_setDatePickerType(this.options[this.selectedIndex].value)"> > * > * **<option value="QUICK" selected>Quick Date Range:</option>* > * **<option value="EXACT">Exact Date Range:</option>* > * **<option value="COMPARE">Compare Date Ranges:</option>* > * **</select>* > * **</td>* > * **<td id="date-picker-quick-selector">* > * **<select name="interval" > onchange="_AWD_PerformanceModule_setSelectedInterval(this.options[this.selectedIndex].value)"> > * > * **<option value="today" selected>Today</option>* > * **<option value="yesterday">Yesterday</option>* > * **<option value="last7days">Last 7 days</option>* > * **<option value="lastweek">Last week (Mon-Sun)</option>* > * **<option value="lastbusinessweek">Last business week (Mon-Fri)</option> > * > * **<option value="thismonth">This month</option>* > * **<option value="lastmonth">Last month</option>* > * **<option value="alltime">All time</option>* > * **</select>* > * **</td>* > * **</tr>* > * **</table>* > *</form>* > > I?ve been trying to the use the following pieces of Ruby code to change the > options in the 2nd list: > > *agent.page.forms[0].set_fields(:interval => "last7days")* > > *page = agent.submit(agent.page.forms[0])* > > > And: > > *agent.page.forms[0].interval = ?last7days?* > > *page = agent.submit(agent.page.forms[0])* > > > Both of these fail to work as I?m expecting. Can anyone suggest some tips > or some examples of how to use correctly use select boxes with Mechanize? > > Best Regards, > Jonathan > > > > > > > > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20100531/70db58f8/attachment-0001.html>
Thanks for forwarding. I didn?t receive your message earlier, I don?t think the list is working for me. I understand, what your suggesting below and have tried to implement it, however, still no joy. Does Mechanize have trouble with AJAX forms? I think the one I?m trying to manipulate might be AJAX because it doesn?t refresh or redirect the page. On 31 May 2010, at 14:35, Justin Brinkerhoff wrote:> Not sure if you got my reply or not earlier, due to your later email asking if we are getting your messages on the list. > > So here is my forwarded response in the event you didn''t get it. > > ---------- Forwarded message ---------- > From: Justin Brinkerhoff <justinbrinkerhoff at gmail.com> > Date: Mon, May 31, 2010 at 11:00 AM > Subject: Re: [Mechanize-users] Mechanize SelectList !?! > To: Ruby Mechanize Users List <mechanize-users at rubyforge.org> > > > Have you taken a look at the API? > > http://mechanize.rubyforge.org/mechanize/Mechanize/Form/SelectList.html > > However, the documentation is not very intuitive. > > If you want to select a specific option of a dropdown on a webpage, you have to use the argument .select. > > Let''s say I want to go to JimBeam.com. The landing page has an age verification form. > > I could use Mechanize to do this for me. > > So I open up irb, and do the following: > > > irb > > require ''rubygems'' > > require ''mechanize'' > > agent = Mechanize.new > > agent.get("http://www.jimbeam.com/lpa") > > form = agent.page.forms.first > > year = form.field("year") # Year select list. > > month = form.field("month") # Month select list > > day = form.field("day") # Day select list > > Ok, now that I have all my variables in place for each of the lists, now I want to tell it to enter my birthdate. Being my birthdate is 3/6/84, I would do the following: > > > year.option_with(:value => /1984/).select # Changes the selected value to 1984 > > month.option_with(:value => /3/).select # Changes selected value to 3 > > day.option_with(:value => /6/).select # Changes selected value to 6 > > Now, I want to submit the form with the new values. So, I''ll do the standard form.submit using my form variable. > > > form.submit > > And there you have it. > > So ultimately, you want to find the selectlist to control, then find the option, and then use the .select method to change the value. > > I hope this helps. > > Thanks, > > Justin > > On Mon, May 31, 2010 at 9:10 AM, <overco at me.com> wrote: > Hi there, > > Firstly, thanks for Mechanize! I?m finding it incredibly useful so far. > > The issue I?m stuck on is to do with select lists, I can?t seem to figure out how they should be accessed and selected. > > Here is the HTML: > > <form action="" method="POST"> > <table id="date-picker-table"> > <tr> > <td class="top-aligned" rowspan="2"> > <select id="date-picker-type-selector" onchange="_AWD_PerformanceModule_setDatePickerType(this.options[this.selectedIndex].value)"> > <option value="QUICK" selected>Quick Date Range:</option> > <option value="EXACT">Exact Date Range:</option> > <option value="COMPARE">Compare Date Ranges:</option> > </select> > </td> > <td id="date-picker-quick-selector"> > <select name="interval" onchange="_AWD_PerformanceModule_setSelectedInterval(this.options[this.selectedIndex].value)"> > <option value="today" selected>Today</option> > <option value="yesterday">Yesterday</option> > <option value="last7days">Last 7 days</option> > <option value="lastweek">Last week (Mon-Sun)</option> > <option value="lastbusinessweek">Last business week (Mon-Fri)</option> > <option value="thismonth">This month</option> > <option value="lastmonth">Last month</option> > <option value="alltime">All time</option> > </select> > </td> > </tr> > </table> > </form> > > I?ve been trying to the use the following pieces of Ruby code to change the options in the 2nd list: > > agent.page.forms[0].set_fields(:interval => "last7days") > page = agent.submit(agent.page.forms[0]) > > And: > > agent.page.forms[0].interval = ?last7days? > page = agent.submit(agent.page.forms[0]) > > Both of these fail to work as I?m expecting. Can anyone suggest some tips or some examples of how to use correctly use select boxes with Mechanize? > > Best Regards, > Jonathan > > > > > > > > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users > >Jonathan Clarke ? Campaign Manager U365 Cell Number: +1-246-232-0770 Skype IM Chat: jonathan.clarke This message is intended only for the addressee and contains privileged and confidential information. If you have received this message in error please notify me immediately and delete the original message and destroy any copies of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20100531/a28f1639/attachment.html>