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

Hi, Hmmmm almost I am working for this for the last 3 days. But could not able to solve the issue. Actually I am using perl mechanize module for some authenticated form submission. I was able to login and I was able to do a lot. At final stage when I was submiting one form Is is unable to post few default values because they are filled by javascript when the page loads. So I searched for mechanize with javascript which was not working and finally what I did is I am able to parse the option names and values by reading the html code.

Html code that will generated once the javascript executes
--------------------------------------------------------
<select id="samba" ondblclick="javascript:ddddd('rwww');"
size="20" multiple="" name="samba">
<option value="172">X</option>
<option value="45">Y</option>
<option value="39">Z</option>
<option value="56">M</option>
</select>

At this point of time I am able to post only one value like this.
$mech->submit_form(
form_number => 1,
fields => {
loginid => 'testid8',
password => 'testid8',
repassword => 'testid8',
name => 'Testing',
email => samba@gmail.com',
samba => '45',
},
);

But when my question is how can I pass multiple values. Tried in different ways but no luck. I could not able to use $mech->select as the above code will be generated after the java script excutes which is not happening in mechanize. Plese help me....

Replies are listed 'Best First'.
Re: Please help me in mechanize module
by Corion (Patriarch) on Sep 03, 2010 at 13:59 UTC
      >>
      (see WWW::Mechanize about the ->set_content method)
      I used update_html this..
      my $string="@groupstring"; my $html = $mech->content; $html =~ s/\"javascript:sbtn\(\'r\'\)\;\"><\/select>/\"javascript:sbtn\(\'r\'\)\;\">$string<\/select>"/isg; $mech->update_html( $html ); $mech->save_content('C:/Documents and Settings/samba/Desktop/x_new.html'); Can you please guide me how to use WWW::Scripter? or I am almost done parsing the option values and names in selecte statement. pls guide me how I can pass the multiple values manually.
      form_number => 1,
      fields => {
      loginid => 'testid8',
      password => 'testid8',
      repassword => 'testid8',
      name => 'Testing',
      email => samba@gmail.com',
      samba => '45',
      },
      );
      In the above lines if you see "samba => '45'" here I am passing the value for "samba" directly but I want to know
      how I can pass multiple values like "samba => '45','172','39'"
Re: Please help me in mechanize module
by marto (Cardinal) on Sep 03, 2010 at 14:13 UTC

    "I could not able to use $mech->select as the above code will be generated after the java script excutes which is not happening in mechanize."

    Both the WWW::Mechanize documentation and the FAQ explain that it doesn't support JavaScript. I'll second Corions suggestion of using WWW::Mechanize::Firefox.

      >>
      (see WWW::Mechanize about the ->set_content method)
      I used update_html this..
      my $string="@groupstring"; my $html = $mech->content; $html =~ s/\"javascript:sbtn\(\'r\'\)\;\"><\/select>/\"javascript:sbtn\(\'r\'\)\;\">$string<\/select>"/isg; $mech->update_html( $html ); $mech->save_content('C:/Documents and Settings/samba/Desktop/x_new.html'); Can you please guide me how to use WWW::Scripter? or I am almost done parsing the option values and names in selecte statement. pls guide me how I can pass the multiple values manually.
      form_number => 1,
      fields => {
      loginid => 'testid8',
      password => 'testid8',
      repassword => 'testid8',
      name => 'Testing',
      email => samba@gmail.com',
      samba => '45',
      },
      );
      In the above lines if you see "samba => '45'" here I am passing the value for "samba" directly but I want to know
      how I can pass multiple values like "samba => '45','172','39'"
        samba => '45','172','39'

        is definitely wrong since it's the same as

        samba => '45', 172 => '39',

        If it's supported, I would guess you'd have to use

        samba => [ '45','172','39' ]
        A reply falls below the community's threshold of quality. You may see it by logging in.
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Please help me in mechanize module
by ikegami (Patriarch) on Sep 03, 2010 at 19:09 UTC

    Since you know what needs to be sent and it doesn't necessarily jive with the form, you're better off bypassing the normal form logic by calling ->get or ->post directly. (->post is inherited from LWP::UserAgent.)

    $mech->post( $uri, [ ..., email => 'samba@gmail.com', samba => '45', samba => '172', samba => '39', ], );
    If the list of numbers isn't constant, the following might be more useful:
    my @sambas = ('45', 172', '39'); $mech->post( $uri, [ ..., email => 'samba@gmail.com', ( map { samba => $_ } @sambas ), ], );
      thanks a lot ikegami!..... You solved my problem.. thanks a ton
Re: Please help me in mechanize module
by choroba (Cardinal) on Sep 03, 2010 at 17:14 UTC
    Reading the documentation, I guess you should $mech->tick('samba',$_) for qw/45 172 39/ before submitting the form.