Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

First Mech script: entering a form value (another simple thing no doubt)

3 views
Skip to first unread message

G M

unread,
Mar 14, 2013, 11:27:53 AM3/14/13
to begi...@perl.org

Hi all,

I managed to get the problem with my script not connecting to the page last night, turned out my web host wouldn't allow it. Got that sorted. Filling in the form should be really simple but I'm getting the following error when trying to set the "acOriginAirport" field, I thought it might be to do with the javascript on the page but I'm entering a value that the form accepts.

The form is being found on the page as I can dump out $agent->forms();

Any ideas anyone?

Status: 500
Content-type: text/html

Software error:
Can't call method "value" on an undefined value at /usr/local/lib/perl5/site_perl/5.8.8/WWW/Mechanize.pm line 1407.


This is my script:
#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use WWW::Mechanize;
use HTML::TokeParser;
use Data::Dumper;
print "Content-type: text/html\n\n";
print "setting up mech<br />";
my $agent = WWW::Mechanize->new();
$agent->agent_alias('Windows Mozilla');
print "mech setup";
$agent->get('http://www.easyjet.com/en/searchpod.mvc/showborderless?aclwidth=279');
print "setting up airports <br />";
$agent->field("acOriginAirport", "Glasgow GLA");




Cheers in advance,

G :)

Jim Gibson

unread,
Mar 14, 2013, 12:27:27 PM3/14/13
to Perl Beginners
I have not used WWW::Mechanize, but from reading the documentation, it would seem you must call $agent->form_number($number) with a form number before making any calls to field().

Line 1407 of Mechanize.pm is part of the field method:

sub field {
my ($self, $name, $value, $number) = @_;
$number ||= 1;

my $form = $self->current_form();
if ($number > 1) {
$form->find_input($name, undef, $number)->value($value);
}
else {
if ( ref($value) eq 'ARRAY' ) {
$form->param($name, $value);
}
else {
$form->value($name => $value); # line 1407
}
}
}

The current_form() method is returning null because you have not specified (with a call to form_number()) which form you are using. Hence the error message you are seeing.



David Precious

unread,
Mar 14, 2013, 12:56:37 PM3/14/13
to begi...@perl.org
On Thu, 14 Mar 2013 16:42:52 +0000
G M <iamnotre...@hotmail.com> wrote:

>
> Hi,
>
> Thanks for replying. As far as I understand it will return undef if
> no form is found. The form is there so it should do something I
> would've though, I tried adding in that line anyway using 1 to
> signify the first (and only) form on the page, still get the same
> error though :(

There are no forms on the page.

DB<6> $mech = WWW::Mechanize->new;
DB<7> $mech->get(
'http://www.easyjet.com/en/searchpod.mvc/showborderless?aclwidth=279');
DB<8> x $mech->forms;
empty array

See also:

[davidp@supernova:~]$ wget -q -O-
http://www.easyjet.com/en/searchpod.mvc/showborderless?aclwidth=279 |
grep -i '<form'
[davidp@supernova:~]$

WWW::Mechanize wants to be able to find a form to actually submit.

You may be able to just fake the request yourself - at a quick glance,
it looks like it submits to http://www.easyjet.com/EN/Booking.mvc, so
take the field names from the source of the URL you gave before, and
POST them at that URL, and see what happens.

Failing that, you'll have to use the form in your browser while using
an addon like Firebug or something, or capturing HTTP traffic off the
wire, and see what happens, and make your script do the same.



--
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github


0 new messages