DavidGamba
DavidGamba

Reputation: 3623

using Perl and WWW::Mechanize to get HTML with filled-in forms and save it to file

I am trying to get a session recording of everything I do using WWW::Mechanize. It is really important to have the HTML with the input fields filled and saved to a file.

My code:

$m->save_content($file); # Saves the page without any forms filled

$m->field('user_name', $user); #fills the form
# I need to save the html with the filled form
# $m->save_content($file_2); won't do it
# $m->dump_forms; shows that mechanize knows about the filled forms

$m->click('SUBMIT.x');
$m->save_content($file); # Too late, already in a different page

Any ideas? I have seen it working with LWP::UserAgent but I don't have access to the code.

I have tried everything I can come up with but nothing will update content with the values of $m->forms().

EDIT: basically what I want is to have a function of the type:

$updated_content = merge($m->content, $m->dump_forms);

So when I save it I can see what the input that was given to the forms into a html slideshow.

I don't need to save the current state of the object or restore the session after it is closed.

Upvotes: 2

Views: 1171

Answers (1)

Borodin
Borodin

Reputation: 126772

The solution depends on what you are trying to achieve. The save_content method saves only the content of the last HTTP response and not the entire WWW::Mechanize state.

If you want to store a WWW::Mechanize object so that browsing can proceed at any time from a given point, then you need to investigate serializing a blessed object.

My choice would be to use Data::Dump. If you write

use Data::Dump 'dump';
use WWW::Mechanize;

my $mech = WWW::Mechanize->new;
$mech->get('http://www.mysite.com/path/resource.html');
$mech->form_with_fields(qw/ username password /);
$mech->set_fields( username => 'me', password => 'secret');

open my $dump, '>', 'mechanize_freeze.pl' or die $!;
print { $dump } dump $mech;
close $dump or die $!;

… then you should have a file that you can restore in a separate program using

my $oldmech = do 'mechanize_freeze.pl';

Upvotes: 2

Related Questions