mbr has asked for the wisdom of the Perl Monks concerning the following question:
I have need of writing a perl program to interact with an html
form that includes radio buttons. Unfortunately, I do not
have control over the form, and those who wrote it forgot to close
the form with a "/form" tag within their html code. (So the
form is started with the proper "form" tag, but is never ended
with the "/form" tag.)
Following the code in the HTTP::Request::Form documentation,
I tried doing something like this:
use URI::URL;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common qw(POST);
use HTTP::Request::Form;
use HTML::TreeBuilder 3.0;
use Data::Dumper;
my $ua = LWP::UserAgent->new();
my $url = 'http://formhost/form.cgi';
my $req = $ua->request(GET $url);
my $tree = HTML::TreeBuilder->new();
$tree->parse($req->content);
$tree->eof();
my @forms = $tree->find_by_tag_name('form');
die "No forms found in $url\n" unless @forms;
print Dumper @forms;
exit 0;
This code always dies because nothing shows up in the @forms
array due (I think) to the fact that find_by_tag_name('form')
is unable to find the closing tag.
How can I interact with the radio buttons? Can I just use
POST in HTTP::Request::Common in some way? Or, is there some
other function within HTML::TreeBuilder that I can use?
Before you call parse(), can you append a closing form tag? If you know where it's supposed to be, you could use a regex, or you could just say:
$tree->parse($req->content . '</form>');
Low tech, but sometimes it's enough.
Hmmm, that will probably work. I guess I will actually have
to append '</form>' before the closing '</body>' and '</html>'
tags, but that should not be too hard to do. Thank you very much.
--Mike