Question

I am trying to gather data from a website. Some anti-patterns make looking finding the right form objects difficult but I have this solved. I am using a post method to get around some javascript acting as a wrapper to submit the form. My problem seems to be in getting the results from the mechanize->post method.

Here's a shortened version of my code.

use strict;
use warnings;
use HTML::Tree;
use LWP::Simple;
use WWW::Mechanize;
use HTTP::Request::Common;
use Data::Dumper;
$| = 1;

my $site_url = "http://someURL"; 
my $mech = WWW::Mechanize->new( autocheck => 1 );
foreach my $number (@numbers) 
{
    my $content = get($site_url);
       $mech->get ($site_url);

    my $tree = HTML::Tree->new();

    $tree->parse($content);

    my ($title) = $tree->look_down( '_tag' , 'a' );
    my $atag = "";
    my $atag1 = "";
    foreach $atag ( $tree->look_down( _tag => q{a}, 'class' => 'button', 'title' => 'SEARCH'     )  ) 
    {
        print "Tag is ", $atag->attr('id'), "\n";
        $atag1 = Dumper $atag->attr('id');
    }

# Enter permit number in "Number" search field
    my @forms = $mech->forms;
    my @fields = ();
    foreach my $form (@forms)
    {
        @fields = $form->param;
    }
    my ($name, $fnumber) = $fields[2];
    print "field name and number is $name\n";
    $mech->field( $name, $number, $fnumber );
    print "field $name populated with search data $number\n" if $mech->success();

    $mech->post($site_url , 
    [
       '$atag1' => $number,
       'internal.wdk.wdkCommand' => $atag1,
    ]) ;

print $mech->content; # I think this is where the problem is.

}

The data I get from my final print statement is the data from teh original URL not the page the POST command should take me to. What have I done wrong?

Many Thanks

Update

I don't have Firefox installed so I'm avoiding WWW::Mechanize::Firefox intentionally.

Was it helpful?

Solution

Turns out I was excluding some required hidden fields from my POST command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top