B
Bryan Balfour
hi, I'm new to perl and have been trying to get into using
WWW::Mechanize on WindowsXP. I'm stuck on trying to use it to submit a
username and password. The following snippet of code shows what I'm
trying to do:
my($mech) = WWW::Mechanize->new(autocheck => 1,
cookie_jar => $cookieJar);
$mech->get($url);
$mech->follow_link(text => "Begin");
if($mech->content() =~ m/Please log in./)
{
print("***** Please log in. *****\n");
outputContent($mech->content(), "Login.html");
$mech->set_visible($userName, $password);
my($response) = $mech->click();
if($response->content() =~ m/Please try again/)
{
print("Logon details incorrect\n");
}
outputContent($response->content(), "Loggedin.html");
}
sub outputContent
{
my($content, $fileName) = @_;
my($tree) = HTML::TreeBuilder->new;
$tree->ignore_ignorable_whitespace(0);
$tree->no_space_compacting(1);
$tree->parse($content) || die $!;
open(OUT, ">$fileName") || die "Can't write: $!";
print OUT $tree->as_HTML;
close(OUT);
$tree->delete;
}
I'm deliberately setting an incorrect password so expect to see the
messages:
***** Please log in. *****
Logon details incorrect
I'm getting the first one but not the second. Conclusion: I've got a
problem with the 'set_visible' or 'click' methods.
The 2 files output by outputContent are identical and are the html for
entering username and password. Looking at these two input fields, they
are NOT defined on a form but in a table. (Is the html treated as one
form by default?)
I've tried using other Mechanize methods such as:
$mech->form_number(1);
$mech->field('user', $userName);
$mech->field('password', $password);
my($response) = $mech->click('submit');
but get the same result. (Curiously, the page contains only one form at
the beginning but that is hidden. The above code generates no error
messages so what form is form_number(1)?)
Am I missing a step as the 'click' method is not returning a new
response but the old one?
I'd appreciate your comments, hints etc.
Bryan
WWW::Mechanize on WindowsXP. I'm stuck on trying to use it to submit a
username and password. The following snippet of code shows what I'm
trying to do:
my($mech) = WWW::Mechanize->new(autocheck => 1,
cookie_jar => $cookieJar);
$mech->get($url);
$mech->follow_link(text => "Begin");
if($mech->content() =~ m/Please log in./)
{
print("***** Please log in. *****\n");
outputContent($mech->content(), "Login.html");
$mech->set_visible($userName, $password);
my($response) = $mech->click();
if($response->content() =~ m/Please try again/)
{
print("Logon details incorrect\n");
}
outputContent($response->content(), "Loggedin.html");
}
sub outputContent
{
my($content, $fileName) = @_;
my($tree) = HTML::TreeBuilder->new;
$tree->ignore_ignorable_whitespace(0);
$tree->no_space_compacting(1);
$tree->parse($content) || die $!;
open(OUT, ">$fileName") || die "Can't write: $!";
print OUT $tree->as_HTML;
close(OUT);
$tree->delete;
}
I'm deliberately setting an incorrect password so expect to see the
messages:
***** Please log in. *****
Logon details incorrect
I'm getting the first one but not the second. Conclusion: I've got a
problem with the 'set_visible' or 'click' methods.
The 2 files output by outputContent are identical and are the html for
entering username and password. Looking at these two input fields, they
are NOT defined on a form but in a table. (Is the html treated as one
form by default?)
I've tried using other Mechanize methods such as:
$mech->form_number(1);
$mech->field('user', $userName);
$mech->field('password', $password);
my($response) = $mech->click('submit');
but get the same result. (Curiously, the page contains only one form at
the beginning but that is hidden. The above code generates no error
messages so what form is form_number(1)?)
Am I missing a step as the 'click' method is not returning a new
response but the old one?
I'd appreciate your comments, hints etc.
Bryan