man WWW::Mechanize::FAQ () - Frequently Asked Questions about WWW::Mechanize

NAME

WWW::Mechanize::FAQ - Frequently Asked Questions about WWW::Mechanize

How do I do X?

Can I do [such-and-such] with WWW::Mechanize?

If it's possible with LWP::UserAgent, then yes. WWW::Mechanize is a subclass of LWP::UserAgent, so all the wondrous magic of that class is inherited.

How do I use WWW::Mechanize through a proxy server?

See the docs in LWP::UserAgent on how to use the proxy. Short version:

    $mech->proxy(['http', 'ftp'], 'http://proxy.example.com:8000/');

or get the specs from the environment:

    $mech->env_proxy();

    # Environment set like so:
    gopher_proxy=http://proxy.my.place/
    wais_proxy=http://proxy.my.place/
    no_proxy="localhost,my.domain"
    export gopher_proxy wais_proxy no_proxy

How can I see what fields are on the forms?

Use the mech-dump utility, optionaly installed with Mechanize.

    $ mech-dump --forms http://search.cpan.org
    Dumping forms
    GET http://search.cpan.org/search
      query=
      mode=all                        (option)  [*all|module|dist|author]
      <NONAME>=CPAN Search            (submit)

How do I get Mech to handle authentication?

    my $agent = WWW::Mechanize->new();
    my @args = (
        Authorization => "Basic " .
            MIME::Base64::encode( USER . ':' . PASS )
    );

    $agent->credentials( ADDRESS, REALM, USER, PASS );
    $agent->get( URL, @args );

How can I get WWW::Mechanize to execute this JavaScript?

You can't. JavaScript is entirely client-based, and WWW::Mechanize is a client that doesn't understand JavaScript.

How do I handle frames?

You don't deal with them as frames, per se, but as links. Extract them with

    my @frame_links = $mech->find_link( tag => "frame" );

How do I get a list of HTTP headers and their values?

All HTTP::Headers methods work on a HTTP::Response object which is returned by the get(), reload(), response()/res(), click(), submit_form(), and request() methods.

    my $mech = WWW::Mechanize->new( autocheck => 1 );
    $mech->get( 'http://my.site.com' );
    my $res = $mech->response();
    for my $key ( $response->header_field_names() ) {
        print $key, " : ", $response->header( $key ), "\n";
    }

Why doesn't this work?

Why don't https:// URLs work?

You need either IO::Socket::SSL or Crypt::SSLeay installed. You're trying to change the value of a hidden field and you have warnings on.

First, make sure that you actually mean to change the field that you're changing, and that you don't have a typo. Usually, hidden variables are set by the site you're working on for a reason. If you change the value, you might be breaking some functionality by faking it out.

If you really do want to change a hidden value, make the changes in a scope that has warnings turned off:

    {
    local $^W = 0;
    $agent->field( name => $value );
    }

I tried to [such-and-such] and I got this weird error.

Are you checking your errors?

Are you sure?

Are you checking that your action succeeded after every action?

Are you sure?

For example, if you try this:

    $mech->get( "http://my.site.com" );
    $mech->follow_link( "foo" );

and the CWget call fails for some reason, then the Mech internals will be unusable for the CWfollow_link and you'll get a weird error. You must, after every action that GETs or POSTs a page, check that Mech succeeded, or all bets are off.

    $mech->get( "http://my.site.com" );
    die "Can't even get the home page: ", $mech->response->status_line
        unless $mech->success;

    $mech->follow_link( "foo" );
    die "Foo link failed: ", $mech->response->status_line
        unless $mech->success;

Author

Copyright 2005 Andy Lester CW<andy@petdance.com>