.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
. if \nF \{
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "WWW::Mechanize::FAQ 3"
.TH WWW::Mechanize::FAQ 3 "2019-08-23" "perl v5.16.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
WWW::Mechanize::FAQ \- Frequently Asked Questions about WWW::Mechanize
.SH "VERSION"
.IX Header "VERSION"
version 1.92
.SH "How to get help with WWW::Mechanize"
.IX Header "How to get help with WWW::Mechanize"
If your question isn't answered here in the \s-1FAQ,\s0 please turn to the
communities at:
.IP "\(bu" 4
StackOverflow <https://stackoverflow.com/questions/tagged/www\-mechanize>
.IP "\(bu" 4
#lwp on irc.perl.org
.IP "\(bu" 4
<http://perlmonks.org>
.IP "\(bu" 4
The libwww-perl mailing list at <http://lists.perl.org>
.SH "JavaScript"
.IX Header "JavaScript"
.SS "I have this web page that has JavaScript on it, and my Mech program doesn't work."
.IX Subsection "I have this web page that has JavaScript on it, and my Mech program doesn't work."
That's because WWW::Mechanize doesn't operate on the JavaScript. It only
understands the \s-1HTML\s0 parts of the page.
.SS "I thought Mech was supposed to work like a web browser."
.IX Subsection "I thought Mech was supposed to work like a web browser."
It does pretty much, but it doesn't support JavaScript.
.PP
I added some basic attempts at picking up URLs in \f(CW\*(C`window.open()\*(C'\fR
calls and return them in \f(CW\*(C`$mech\->links\*(C'\fR. They work sometimes.
.PP
Since Javascript is completely visible to the client, it cannot be used
to prevent a scraper from following links. But it can make life difficult. If
you want to scrape specific pages, then a solution is always possible.
.PP
One typical use of Javascript is to perform argument checking before
posting to the server. The \s-1URL\s0 you want is probably just buried in the
Javascript function. Do a regular expression match on
\&\f(CW\*(C`$mech\->content()\*(C'\fR
to find the link that you want and \f(CW\*(C`$mech\->get\*(C'\fR it directly (this
assumes that you know what you are looking for in advance).
.PP
In more difficult cases, the Javascript is used for \s-1URL\s0 mangling to
satisfy the needs of some middleware. In this case you need to figure
out what the Javascript is doing (why are these URLs always really
long?). There is probably some function with one or more arguments which
calculates the new \s-1URL.\s0 Step one: using your favorite browser, get the
before and after URLs and save them to files. Edit each file, converting
the argument separators ('?', '&' or ';') into newlines. Now it is
easy to use diff or comm to find out what Javascript did to the \s-1URL.\s0
Step 2 \- find the function call which created the \s-1URL \-\s0 you will need
to parse and interpret its argument list. The Javascript Debugger in the
Firebug extension for Firefox helps with the analysis. At this point, it is
fairly trivial to write your own function which emulates the Javascript
for the pages you want to process.
.PP
Here's another approach that answers the question, \*(L"It works in Firefox,
but why not Mech?\*(R" Everything the web server knows about the client is
present in the \s-1HTTP\s0 request. If two requests are identical, the results
should be identical. So the real question is \*(L"What is different between
the mech request and the Firefox request?\*(R"
.PP
The Firefox extension \*(L"Tamper Data\*(R" is an effective tool for examining
the headers of the requests to the server. Compare that with what \s-1LWP\s0
is sending. Once the two are identical, the action of the server should
be the same as well.
.PP
I say \*(L"should\*(R", because this is an oversimplification \- some values
are naturally unique, e.g. a SessionID, but if a SessionID is present,
that is probably sufficient, even though the value will be different
between the \s-1LWP\s0 request and the Firefox request. The server could use
the session to store information which is troublesome, but that's not
the first place to look (and highly unlikely to be relevant when you
are requesting the login page of your site).
.PP
Generally the problem is to be found in missing or incorrect \s-1POSTDATA\s0
arguments, Cookies, User-Agents, Accepts, etc. If you are using mech,
then redirects and cookies should not be a problem, but are listed here
for completeness. If you are missing headers, \f(CW\*(C`$mech\->add_header\*(C'\fR
can be used to add the headers that you need.
.SS "Which modules work like Mechanize and have JavaScript support?"
.IX Subsection "Which modules work like Mechanize and have JavaScript support?"
In no particular order: Gtk2::WebKit::Mechanize, Win32::IE::Mechanize,
WWW::Mechanize::Firefox, WWW::Scripter, WWW::Selenium
.SH "How do I do X?"
.IX Header "How do I do X?"
.SS "Can I do [such\-and\-such] with WWW::Mechanize?"
.IX Subsection "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.
.SS "How do I use WWW::Mechanize through a proxy server?"
.IX Subsection "How do I use WWW::Mechanize through a proxy server?"
See the docs in LWP::UserAgent on how to use the proxy. Short version:
.PP
.Vb 1
\& $mech\->proxy([\*(Aqhttp\*(Aq, \*(Aqftp\*(Aq], \*(Aqhttp://proxy.example.com:8000/\*(Aq);
.Ve
.PP
or get the specs from the environment:
.PP
.Vb 1
\& $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
.Ve
.SS "How can I see what fields are on the forms?"
.IX Subsection "How can I see what fields are on the forms?"
Use the mech-dump utility, optionally installed with Mechanize.
.PP
.Vb 6
\& $ 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)
.Ve
.SS "How do I get Mech to handle authentication?"
.IX Subsection "How do I get Mech to handle authentication?"
.Vb 1
\& use MIME::Base64;
\&
\& my $agent = WWW::Mechanize\->new();
\& my @args = (
\& Authorization => "Basic " .
\& MIME::Base64::encode( USER . \*(Aq:\*(Aq . PASS )
\& );
\&
\& $agent\->credentials( ADDRESS, REALM, USER, PASS );
\& $agent\->get( URL, @args );
.Ve
.PP
If you want to use the credentials for all future requests, you can
also use the LWP::UserAgent \f(CW\*(C`default_header()\*(C'\fR method instead
of the extra arguments to \f(CW\*(C`get()\*(C'\fR
.PP
.Vb 2
\& $mech\->default_header(
\& Authorization => \*(AqBasic \*(Aq . encode_base64( USER . \*(Aq:\*(Aq . PASSWORD ) );
.Ve
.SS "How can I get WWW::Mechanize to execute this JavaScript?"
.IX Subsection "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. See the top part
of this \s-1FAQ.\s0
.SS "How do I check a checkbox that doesn't have a value defined?"
.IX Subsection "How do I check a checkbox that doesn't have a value defined?"
Set it to the value of \*(L"on\*(R".
.PP
.Vb 1
\& $mech\->field( my_checkbox => \*(Aqon\*(Aq );
.Ve
.SS "How do I handle frames?"
.IX Subsection "How do I handle frames?"
You don't deal with them as frames, per se, but as links. Extract
them with
.PP
.Vb 1
\& my @frame_links = $mech\->find_link( tag => "frame" );
.Ve
.SS "How do I get a list of \s-1HTTP\s0 headers and their values?"
.IX Subsection "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 \fI\fIget()\fI\fR, \fI\fIreload()\fI\fR, \fI\fIresponse()\fI/\fIres()\fI\fR, \fI\fIclick()\fI\fR,
\&\fI\fIsubmit_form()\fI\fR, and \fI\fIrequest()\fI\fR methods.
.PP
.Vb 6
\& my $mech = WWW::Mechanize\->new( autocheck => 1 );
\& $mech\->get( \*(Aqhttp://my.site.com\*(Aq );
\& my $response = $mech\->response();
\& for my $key ( $response\->header_field_names() ) {
\& print $key, " : ", $response\->header( $key ), "\en";
\& }
.Ve
.SS "How do I enable keep-alive?"
.IX Subsection "How do I enable keep-alive?"
Since WWW::Mechanize is a subclass of LWP::UserAgent, you can
use the same mechanism to enable keep-alive:
.PP
.Vb 3
\& use LWP::ConnCache;
\& ...
\& $mech\->conn_cache(LWP::ConnCache\->new);
.Ve
.SS "How can I change/specify the action parameter of an \s-1HTML\s0 form?"
.IX Subsection "How can I change/specify the action parameter of an HTML form?"
You can access the action of the form by utilizing the HTML::Form
object returned from one of the specifying form methods.
.PP
Using \f(CW\*(C`$mech\->form_number($number)\*(C'\fR:
.PP
.Vb 4
\& my $mech = WWW::mechanize\->new;
\& $mech\->get(\*(Aqhttp://someurlhere.com\*(Aq);
\& # Access the form using its Zero\-Based Index by DOM order
\& $mech\->form_number(0)\->action(\*(Aqhttp://newAction\*(Aq); #ABS URL
.Ve
.PP
Using \f(CW\*(C`$mech\->form_name($number)\*(C'\fR:
.PP
.Vb 4
\& my $mech = WWW::mechanize\->new;
\& $mech\->get(\*(Aqhttp://someurlhere.com\*(Aq);
\& #Access the form using its Zero\-Based Index by DOM order
\& $mech\->form_name(\*(AqtrgForm\*(Aq)\->action(\*(Aqhttp://newAction\*(Aq); #ABS URL
.Ve
.SS "How do I save an image? How do I save a large tarball?"
.IX Subsection "How do I save an image? How do I save a large tarball?"
An image is just content. You get the image and save it.
.PP
.Vb 2
\& $mech\->get( \*(Aqphoto.jpg\*(Aq );
\& $mech\->save_content( \*(Aq/path/to/my/directory/photo.jpg\*(Aq );
.Ve
.PP
You can also save any content directly to disk using the \f(CW\*(C`:content_file\*(C'\fR
flag to \f(CW\*(C`get()\*(C'\fR, which is part of LWP::UserAgent.
.PP
.Vb 2
\& $mech\->get( \*(Aqhttp://www.cpan.org/src/stable.tar.gz\*(Aq,
\& \*(Aq:content_file\*(Aq => \*(Aqstable.tar.gz\*(Aq );
.Ve
.ie n .SS "How do I pick a specific value from a ""<select>"" list?"
.el .SS "How do I pick a specific value from a \f(CW<select>\fP list?"
.IX Subsection "How do I pick a specific value from a <select> list?"
Find the \f(CW\*(C`HTML::Form::ListInput\*(C'\fR in the page.
.PP
.Vb 1
\& my ($listbox) = $mech\->find_all_inputs( name => \*(Aqlistbox\*(Aq );
.Ve
.PP
Then create a hash for the lookup:
.PP
.Vb 3
\& my %name_lookup;
\& @name_lookup{ $listbox\->value_names } = $listbox\->possible_values;
\& my $value = $name_lookup{ \*(AqName I want\*(Aq };
.Ve
.PP
If you have duplicate names, this method won't work, and you'll
have to loop over \f(CW\*(C`$listbox\->value_names\*(C'\fR and
\&\f(CW\*(C`$listbox\->possible_values\*(C'\fR in parallel until you find a
matching name.
.SS "How do I get Mech to not follow redirects?"
.IX Subsection "How do I get Mech to not follow redirects?"
You use functionality in LWP::UserAgent, not Mech itself.
.PP
.Vb 1
\& $mech\->requests_redirectable( [] );
.Ve
.PP
Or you can set \f(CW\*(C`max_redirect\*(C'\fR:
.PP
.Vb 1
\& $mech\->max_redirect( 0 );
.Ve
.PP
Both these options can also be set in the constructor. Mech doesn't
understand them, so will pass them through to the LWP::UserAgent
constructor.
.SH "Why doesn't this work: Debugging your Mechanize program"
.IX Header "Why doesn't this work: Debugging your Mechanize program"
.SS "My Mech program doesn't work, but it works in the browser."
.IX Subsection "My Mech program doesn't work, but it works in the browser."
Mechanize acts like a browser, but apparently something you're doing
is not matching the browser's behavior. Maybe it's expecting a
certain web client, or maybe you've not handling a field properly.
For some reason, your Mech problem isn't doing exactly what the
browser is doing, and when you find that, you'll have the answer.
.SS "My Mech program gets these 500 errors."
.IX Subsection "My Mech program gets these 500 errors."
A 500 error from the web server says that the program on the server
side died. Probably the web server program was expecting certain
inputs that you didn't supply, and instead of handling it nicely,
the program died.
.PP
Whatever the cause of the 500 error, if it works in the browser,
but not in your Mech program, you're not acting like the browser.
See the previous question.
.SS "Why doesn't my program handle this form correctly?"
.IX Subsection "Why doesn't my program handle this form correctly?"
Run \fImech-dump\fR on your page and see what it says.
.PP
\&\fImech-dump\fR is a marvelous diagnostic tool for figuring out what forms
and fields are on the page. Say you're scraping \s-1CNN\s0.com, you'd get this:
.PP
.Vb 7
\& $ mech\-dump http://www.cnn.com/
\& GET http://search.cnn.com/cnn/search
\& source=cnn (hidden readonly)
\& invocationType=search/top (hidden readonly)
\& sites=web (radio) [*web/The Web ??|cnn/CNN.com ??]
\& query= (text)
\& <NONAME>=Search (submit)
\&
\& POST http://cgi.money.cnn.com/servlets/quote_redirect
\& query= (text)
\& <NONAME>=GET (submit)
\&
\& POST http://polls.cnn.com/poll
\& poll_id=2112 (hidden readonly)
\& question_1=<UNDEF> (radio) [1/Simplistic option|2/VIEW RESULTS]
\& <NONAME>=VOTE (submit)
\&
\& GET http://search.cnn.com/cnn/search
\& source=cnn (hidden readonly)
\& invocationType=search/bottom (hidden readonly)
\& sites=web (radio) [*web/??CNN.com|cnn/??]
\& query= (text)
\& <NONAME>=Search (submit)
.Ve
.PP
Four forms, including the first one duplicated at the end. All the
fields, all their defaults, lovingly generated by HTML::Form's \f(CW\*(C`dump\*(C'\fR
method.
.PP
If you want to run \fImech-dump\fR on something that doesn't lend itself
to a quick \s-1URL\s0 fetch, then use the \f(CW\*(C`save_content()\*(C'\fR method to write
the \s-1HTML\s0 to a file, and run \fImech-dump\fR on the file.
.SS "Why don't https:// URLs work?"
.IX Subsection "Why don't https:// URLs work?"
You need either IO::Socket::SSL or Crypt::SSLeay installed.
.ie n .SS "Why do I get ""Input 'fieldname' is readonly""?"
.el .SS "Why do I get ``Input 'fieldname' is readonly''?"
.IX Subsection "Why do I get Input 'fieldname' is readonly?"
You're trying to change the value of a hidden field and you have
warnings on.
.PP
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.
.PP
If you really do want to change a hidden value, make the changes in a
scope that has warnings turned off:
.PP
.Vb 4
\& {
\& local $^W = 0;
\& $agent\->field( name => $value );
\& }
.Ve
.SS "I tried to [such\-and\-such] and I got this weird error."
.IX Subsection "I tried to [such-and-such] and I got this weird error."
Are you checking your errors?
.PP
Are you sure?
.PP
Are you checking that your action succeeded after every action?
.PP
Are you sure?
.PP
For example, if you try this:
.PP
.Vb 2
\& $mech\->get( "http://my.site.com" );
\& $mech\->follow_link( "foo" );
.Ve
.PP
and the \f(CW\*(C`get\*(C'\fR call fails for some reason, then the Mech internals
will be unusable for the \f(CW\*(C`follow_link\*(C'\fR and you'll get a weird
error. You \fBmust\fR, after every action that GETs or POSTs a page,
check that Mech succeeded, or all bets are off.
.PP
.Vb 3
\& $mech\->get( "http://my.site.com" );
\& die "Can\*(Aqt 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;
.Ve
.ie n .SS "How do I figure out why ""$mech\->get($url)"" doesn't work?"
.el .SS "How do I figure out why \f(CW$mech\->get($url)\fP doesn't work?"
.IX Subsection "How do I figure out why $mech->get($url) doesn't work?"
There are many reasons why a \f(CW\*(C`get()\*(C'\fR can fail. The server can take
you to someplace you didn't expect. It can generate redirects which are
not properly handled. You can get time-outs. Servers are down more often
than you think! etc, etc, etc. A couple of places to start:
.ie n .IP "1 Check ""$mech\->status()"" after each call" 4
.el .IP "1 Check \f(CW$mech\->status()\fR after each call" 4
.IX Item "1 Check $mech->status() after each call"
.PD 0
.ie n .IP "2 Check the \s-1URL\s0 with ""$mech\->uri()"" to see where you ended up" 4
.el .IP "2 Check the \s-1URL\s0 with \f(CW$mech\->uri()\fR to see where you ended up" 4
.IX Item "2 Check the URL with $mech->uri() to see where you ended up"
.ie n .IP "3 Try debugging with ""LWP::ConsoleLogger""." 4
.el .IP "3 Try debugging with \f(CWLWP::ConsoleLogger\fR." 4
.IX Item "3 Try debugging with LWP::ConsoleLogger."
.PD
.PP
If things are really strange, turn on debugging with
\&\f(CW\*(C`use LWP::ConsoleLogger::Everywhere;\*(C'\fR
Just put this in the main program. This causes \s-1LWP\s0 to print out a trace
of the \s-1HTTP\s0 traffic between client and server and can be used to figure
out what is happening at the protocol level.
.PP
It is also useful to set many traps to verify that processing is
proceeding as expected. A Mech program should always have an \*(L"I didn't
expect to get here\*(R" or \*(L"I don't recognize the page that I am processing\*(R"
case and bail out.
.PP
Since errors can be transient, by the time you notice that the error
has occurred, it might not be possible to reproduce it manually. So
for automated processing it is useful to email yourself the following
information:
.IP "\(bu" 4
where processing is taking place
.IP "\(bu" 4
An Error Message
.IP "\(bu" 4
\&\f(CW$mech\fR\->uri
.IP "\(bu" 4
\&\f(CW$mech\fR\->content
.PP
You can also save the content of the page with \f(CW\*(C`$mech\->save_content( \*(Aqfilename.html\*(Aq );\*(C'\fR
.SS "I submitted a form, but the server ignored everything! I got an empty form back!"
.IX Subsection "I submitted a form, but the server ignored everything! I got an empty form back!"
The post is handled by application software. It is common for \s-1PHP\s0
programmers to use the same file both to display a form and to process
the arguments returned. So the first task of the application programmer
is to decide whether there are arguments to processes. The program can
check whether a particular parameter has been set, whether a hidden
parameter has been set, or whether the submit button has been clicked.
(There are probably other ways that I haven't thought of).
.PP
In any case, if your form is not setting the parameter (e.g. the submit
button) which the web application is keying on (and as an outsider there
is no way to know what it is keying on), it will not notice that the form
has been submitted. Try using \f(CW\*(C`$mech\->click()\*(C'\fR instead of
\&\f(CW\*(C`$mech\->submit()\*(C'\fR or vice-versa.
.SS "I've logged in to the server, but I get 500 errors when I try to get to protected content."
.IX Subsection "I've logged in to the server, but I get 500 errors when I try to get to protected content."
Some web sites use distributed databases for their processing. It
can take a few seconds for the login/session information to percolate
through to all the servers. For human users with their slow reaction
times, this is not a problem, but a Perl script can outrun the server.
So try adding a \f(CWsleep(5)\fR between logging in and actually doing anything
(the optimal delay must be determined experimentally).
.SS "Mech is a big memory pig! I'm running out of \s-1RAM\s0!"
.IX Subsection "Mech is a big memory pig! I'm running out of RAM!"
Mech keeps a history of every page, and the state it was in. It actually
keeps a clone of the full Mech object at every step along the way.
.PP
You can limit this stack size with the \f(CW\*(C`stack_depth\*(C'\fR parm in the \f(CW\*(C`new()\*(C'\fR
constructor. If you set stack_size to 0, Mech will not keep any history.
.SH "AUTHOR"
.IX Header "AUTHOR"
Andy Lester <andy at petdance.com>
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
This software is copyright (c) 2004\-2016 by Andy Lester.
.PP
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.