How to use and debug WWW :: Mechanize?

I am very new to Perl and I am learning on the fly while I try to automate some projects for work. So far it has been a lot of fun.

I am working on a report for a client. I can get this report from a web page that I can access. First, I need to fill out the form with my username, password and select a server from the drop-down list and log in. Secondly, I need to click the link for the report section. Thirdly, you need to fill out a form to create a report.

Here is what I have written so far:

my $mech = WWW::Mechanize->new();
my $url = 'http://X.X.X.X/Console/login/login.aspx';

$mech->get( $url );

$mech->submit_form(
     form_number => 1,
     fields      =>{
        'ctl00$ctl00$cphVeriCentre$cphLogin$txtUser'  => 'someone',
        'ctl00$ctl00$cphVeriCentre$cphLogin$txtPW'    => '12345',
        'ctl00$ctl00$cphVeriCentre$cphLogin$ddlServers'  => 'Live',
     button => 'Sign-In'
   },   
);
die unless ($mech->success);

$mech->dump_forms();

I don’t understand why, but after that I look at what the dump produces and I see the code for the first login page, while I believe that I should have reached the next page after my successful login.

- cookie, ?

, ?

, ,

+5
5

, , , . . ? .

Python Mechanize Perl, .

:

ASP.NET __EVENTTARGET , , .

__doPostBack ('foo'), __EVENTTARGET javascript onclick , javascript, .

python , perl.

def add_event_target(form, target):
    #Creates a new __EVENTTARGET control and adds the value specified
    #.NET doesn't generate this in mechanize for some reason -- suspect maybe is 
    #normally generated by javascript or some useragent thing?
    form.new_control('hidden','__EVENTTARGET',attrs = dict(name='__EVENTTARGET'))
    form.set_all_readonly(False)
    form["__EVENTTARGET"] = target
+6

, . - , , Firebug, , , .

, cookie, . , , ? .

EDIT:

  • WWW:: cookie - .
  • , , , . get()?
  • , , , , HTTP .
+2

Windows, Fiddler, , , Fiddler , script.

, - , Fiddler, , Firebug .

+2

Wireshark - WWW::Mechanize. :

  • , HTTP- .
  • . HTTP.
  • , , , .

HTTP Perl script.

+1

aspx- , , "__" aspxform. , , , .

, , , :

__VIEWSTATE
__EVENTVALIDATION.

- , , , aspxform, , , , , .

aspx, javascript / asp, , , perl mechanize, .

, , :

my $browser = WWW::Mechanize->new( );

# fetch the login page to get the initial session variables
my $login_page = 'http://www.example.com/login.aspx';
$response = $browser->get( $login_page);

# very short way to find the fields so you can add them to your post
$viewstate = ($browser->find_all_inputs( type => 'hidden', name => '__VIEWSTATE' ))[0]->value;
$validation = ($browser->find_all_inputs( type => 'hidden', name => '__EVENTVALIDATION' ))[0]->value;

# post back the formdata you need along with the session variables
$browser->post( $login_page, [ username => 'user', password => 'password, __VIEWSTATE => $viewstate, __EVENTVALIDATION => $validation ]);

# finally get back the content and make sure it looks right
print $response->content();
0

All Articles