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

I have a small script here that uses Mech to find all the fields on the form on the website. The code in question is:
@fields = $mech->find_all_inputs();
Now, what that returns is not that helpful:
HTML::Form::TextInput=HASH(0x87b6ba8) HTML::Form::ListInput=HASH(0x87bae78) HTML::Form::SubmitInput=HASH(0x87bb460) HTML::Form::TextInput=HASH(0x87bf770) HTML::Form::ListInput=HASH(0x87bbd48) HTML::Form::ListInput=HASH(0x87bf740) HTML::Form::SubmitInput=HASH(0x87bf8a8) HTML::Form::TextInput=HASH(0x87c049c) HTML::Form::ListInput=HASH(0x87c0388) HTML::Form::SubmitInput=HASH(0x87c03c4) HTML::Form::TextInput=HASH(0x87ce480) HTML::Form::ListInput=HASH(0x87ce1a4) HTML::Form::SubmitInput=HASH(0x87ce2f4)
So now I know if the field is either a textbox, some sort of list, or a submit button. But the site has both dropdown fields and radio button fields. Anyway to distinguish between them? Is there a better way to gather the field types? Oh, and is there a way to select a dropdown option by its number on the list, rather than what it says?

Replies are listed 'Best First'.
Re: WWW::Mechanize form field types
by Gangabass (Vicar) on Jun 30, 2008 at 04:13 UTC

    I'm sure you'll find answer in HTML::Form documentation.

    $input->type Returns the type of this input. The type is one of the following strin +gs: "text", "password", "hidden", "textarea", "file", "image", "submi +t", "radio", "checkbox" or "option".
      Ah yes, that is exactly what I was looking for =D I guess I'll reply to this if there is any more trouble. Thanks.


      EDIT: It is outputting the same thing =(

      Am I doing something wrong?

      Here is my code relevant to the issue:
      use strict; use WWW::Mechanize; use HTML::Form; use LWP::UserAgent; my $url = "a valid url to an .aspx form here"; my $mech = WWW::Mechanize->new(autocheck => 1); $mech->get( $url ) or die("Could not connect to $url"); my $ua = LWP::UserAgent->new; my $response = $ua->get($url); my @forms = HTML::Form->parse($response); foreach my $page (1..15) { foreach my $form (@forms) { my @fields = ""; @fields = $form->inputs; foreach my $hurr (0..$#fields) { print "$fields[$hurr]\t $page / 15\n"; } } }

        As i see you don't read docs :-(

        foreach my $form (@forms) { my @fields = $form->inputs; foreach my $input (@fields) { print $input->type() . "\t $page / 15\n"; } }
      Don't use pre tags, use code tags.

        Thanks for your above post, it is useful for me. However I'm not able to find any form on my page. I used all form options available with www::mechanize. The page is jsp page and the only form tag available in source code is as mentioned below.

        <form action="html" method="post"> <input type="submit" name="clearAllCaches" value="Clear Al +l Caches" style="margin:1em;" /> </form>
Re: WWW::Mechanize form field types
by codeacrobat (Chaplain) on Jun 30, 2008 at 21:07 UTC
    Try the interactive version of WWW::Mechanize WWW::Mechanize::Shell and use the forms command to debug the inputs.
    # example perl -MWWW::Mechanize::Shell -eshell (no url)>get http://perlmonks.org/?parent=694680;node_id=3333 Retrieving http://perlmonks.org/?parent=694680;node_id=3333(200) http://perlmonks.org/?parent=694680;node_id=3333>forms Form [1] GET http://perlmonks.org/? node= (text) <NONAME>=Search (submit) Form [2] POST http://perlmonks.org/? node_id=3333 (hidden readonly) node=Re: WWW::Mechanize form field types (text) note_doctext= (textarea) type=note (hidden readonly) note_parent_node=694680 (hidden readonly) op=preview (submit) Form [3] POST http://perlmonks.org/? [login] node_id=3333 (hidden readonly) op=login (hidden readonly) lastnode_id=3333 (hidden readonly) user= (text) passwd= (password) expires=<UNDEF> (checkbox) [*<UNDEF>/off|+10y/remembe +r me] login=Login (submit) Form [4] GET http://perlmonks.org/? node_id=3333 (hidden readonly) foo=Refresh (submit) Form [5] POST http://perlmonks.org/? displaytype=display (hidden readonly) node_id=693810 (hidden readonly) vote=0 (radio) [*0/Boss|1/Users|2/Spouse| +3/Doorman|4/Producer|5/Scientists|6/Janitors|7/Secretary|8/Window was +hers|9/Professors|10/Soda/Snack refillers|11/Human resources director +|12/Administrative assistant|13/Security guard|14/Parking attendant|1 +5/Nothing but geeks here|16/I work alone|17/I love everybody|18/What +job?] <NONAME>=Vote (submit)
    Hope this helps.

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
      Ah. Will do when I get back. Thanks :D