triskele has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm using Win32::IE::Mechanize to fill out a web form. One of the fields in the form is designed to allow the user to select multiple options at the same time in one form submission. I'm not seeing how to do this, properly. I'm using set_fields and I've tried

$ie->set_fields(parms=>'var1', parms=>'var2');

and

$ie->set_fields(parms=>'var1');

$ie->set_fields)parms=>'var2');

and neither works. Clearly I am doing something wrong. (I confess to being new to Perl, but not new to programming.) Is it possible this is something 'the Mech' cannot do? (though I doubt that?)

Suggestions? thanks.

  • Comment on multiple selections in the same field using Mechanize

Replies are listed 'Best First'.
Re: multiple selections in the same field using Mechanize
by Anonymous Monk on Dec 08, 2011 at 21:52 UTC

    Suggestions? thanks.

    You can always use the DOM methods ? /duck :D

    Ok, new suggestion

    Instead of trying what you've tried, why not try what is documented?

    http://search.cpan.org/perldoc/Win32::IE::Mechanize#$ie-%3Eset_fields%28_%arguments_%29

    $pie -> set_fields( foo => [qw/ bar bar bar cab /];
      Right. Well, being a bit new, I have trouble parsing what the documentation SAYS; I know enough to read it! But, what you suggest doesn't work. I end up with the field in the form being set to (only) the last entry in the list. It's not clear to me from the documentation that the IE Mech supports the multiple setting of a form field?

        Duuuuuuuuuh %~}

        I read that like 5 times, it is supposed to be

        $pie -> set_fields( foo => [qw/ bar 1/] , foo => [qw/ bar 2/] , foo => [qw/ bar 3/] , foo => [qw/ TAXI 4/] , );

        I guess that DOM suggestion wasn't such a joke

        What a stupidpainful interface , Win32::IE::Form - Mimic HTML::Form

        You (or the author, or both, ) should copy convenience methods from WWW::Mechanize, anything to get away from that HTML::Form verbosity, sheesh

Re: multiple selections in the same field using Mechanize
by keszler (Priest) on Dec 09, 2011 at 01:02 UTC

    Setting a select field with multiple values allowed is documented at http://search.cpan.org/~abeltje/Win32-IE-Mechanize-0.009/lib/Win32/IE/Mechanize.pm#$ie-%3Eselect%28_$name,_\@values_%29:

    $ie->select( $name, \@values ) Given the name of a select field, set its value to the value specified. If the field is not <select multiple> and the $value is an array, only the first value will be set. Passing $value as a hash with an n key selects an item by number (e.g. {n = 3> or {n = 2,4}>). The numbering starts at 1. This applies to the current form (as set by the form() method or defaulting to the first form on the page).
    So,
    $ie->select( 'parms', [ 'var1', 'var2' ]); or @multi_values = ('var1','var2'); $ie->select( 'parms', \@multi_values);
      ++

      :) Your reading comprehension is most excellent

        .. And thanks, A.M., for trying :)

      Thank you, Keszler - that .. nearly works. The multiple fields get highlighted, but then it becomes a fail with this error,

      "Use of unitialized value in list assignment at C:\PERL..\IE\Input.pm line 115"

      ... and this is the entire script:

      ----------------------------

      my $url = "http://www.mesonet.org/index.php/weather/daily_data_retriev +al"; my $ie =Win32::IE::Mechanize->new( visible=> 1 ); my @variables = ('TAVG','DAVG','PAVG','HMAX','HMIN','HAVG','WSPD','ATO +T'); $ie->get($url); sleep 1; $ie->form_name("request"); $ie->set_fields(beginmonth=>'05'); $ie->set_fields(beginday=>'15'); $ie->set_fields(beginyear=>'2010'); $ie->set_fields(endmonth=>'07'); $ie->set_fields(endday=>'06'); $ie->set_fields(endyear=>'2010'); $ie->select( 'parms', \@variables); $ie->set_fields(stid=> $station); $ie->click("SUBMIT"); my $html = $ie->content; print $html; $ie->close;

      -----------------------

      ... any other glaring errors on my part, or is it IE::Mech?

        .. and this is the offending line 115 in Input.pm:

        %vals = map { ( $_ => undef ) } @values;

        But I'm such a newbie to Perl, I can't parse what's in the brackets?