Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / WWW / Mechanize / FAQ.pod
CommitLineData
3fea05b9 1=head1 NAME
2
3WWW::Mechanize::FAQ - Frequently Asked Questions about WWW::Mechanize
4
5=head1 How to get help with WWW::Mechanize
6
7If your question isn't answered here in the FAQ, please turn to the
8communities at:
9
10=over
11
12=item * L<http://perlmonks.org>
13
14=item * The libwww-perl mailing list at L<http://lists.perl.org>
15
16=back
17
18=head1 JavaScript
19
20=head2 I have this web page that has JavaScript on it, and my Mech program doesn't work.
21
22That's because WWW::Mechanize doesn't operate on the JavaScript. It only
23understands the HTML parts of the page.
24
25=head2 I thought Mech was supposed to work like a web browser.
26
27It does pretty much, but it doesn't support JavaScript.
28
29I added some basic attempts at picking up URLs in C<window.open()>
30calls and return them in C<< $mech->links >>. They work sometimes.
31Beyond that, there's no support for JavaScript.
32
33=head2 Are you going to add JavaScript support?
34
35I will if anyone sends me the code to do it. I'm not going to write a
36JavaScript processor myself.
37
38=head2 Wouldn't that be a great thing to have in WWW::Mechanize?
39
40Yes.
41
42=head2 Would it be hard to do?
43
44Hard enough that I don't want to deal with it myself. Plus, I don't
45use JavaScript myself, so I don't have an itch to scratch.
46
47=head2 Is anyone working on it?
48
49I've heard noises from people every so often over the past couple of
50years, but nothing you'd pin your hopes on.
51
52=head2 It would really help me with a project I'm working on.
53
54I'm sure it would.
55
56=head2 Do you know when it might get added?
57
58I have no idea if or when such a thing will ever get done. I can
59guarantee that as soon as there's anything close to JavaScript support
60I will let everyone know.
61
62=head2 Maybe I'll ask around and see if anyone else knows of a solution.
63
64If you must, but I doubt that anyone's written JavaScript support for
65Mechanize and neglected to tell me about it.
66
67=head2 So what can I do?
68
69Since Javascript is completely visible to the client, it cannot be used
70to prevent a scraper from following links. But it can make life difficult,
71and until someone writes a Javascript interpreter for Perl or a Mechanize
72clone to control Firefox, there will be no general solution. But if
73you want to scrape specific pages, then a solution is always possible.
74
75One typical use of Javascript is to perform argument checking before
76posting to the server. The URL you want is probably just buried in the
77Javascript function. Do a regular expression match on
78C<< $mech->content() >>
79to find the link that you want and C<< $mech->get >> it directly (this
80assumes that you know what you are looking for in advance).
81
82In more difficult cases, the Javascript is used for URL mangling to
83satisfy the needs of some middleware. In this case you need to figure
84out what the Javascript is doing (why are these URLs always really
85long?). There is probably some function with one or more arguments which
86calculates the new URL. Step one: using your favorite browser, get the
87before and after URLs and save them to files. Edit each file, converting
88the the argument separators ('?', '&' or ';') into newlines. Now it is
89easy to use diff or comm to find out what Javascript did to the URL.
90Step 2 - find the function call which created the URL - you will need
91to parse and interpret its argument list. Using the Javascript Debugger
92Extension for Firefox may help with the analysis. At this point, it is
93fairly trivial to write your own function which emulates the Javascript
94for the pages you want to process.
95
96Here's annother approach that answers the question, "It works in Firefox,
97but why not Mech?" Everything the web server knows about the client is
98present in the HTTP request. If two requests are identical, the results
99should be identical. So the real question is "What is different between
100the mech request and the Firefox request?"
101
102The Firefox extension "Tamper Data" is an effective tool for examining
103the headers of the requests to the server. Compare that with what LWP
104is sending. Once the two are identical, the action of the server should
105be the same as well.
106
107I say "should", because this is an oversimplification - some values
108are naturally unique, e.g. a SessionID, but if a SessionID is present,
109that is probably sufficient, even though the value will be different
110between the LWP request and the Firefox request. The server could use
111the session to store information which is troublesome, but that's not
112the first place to look (and highly unlikely to be relevant when you
113are requesting the login page of your site).
114
115Generally the problem is to be found in missing or incorrect POSTDATA
116arguments, Cookies, User-Agents, Accepts, etc. If you are using mech,
117then redirects and cookies should not be a problem, but are listed here
118for completeness. If you are missing headers, C<< $mech->add_header >>
119can be used to add the headers that you need.
120
121=head1 How do I do X?
122
123=head2 Can I do [such-and-such] with WWW::Mechanize?
124
125If it's possible with LWP::UserAgent, then yes. WWW::Mechanize is
126a subclass of L<LWP::UserAgent>, so all the wondrous magic of that
127class is inherited.
128
129=head2 How do I use WWW::Mechanize through a proxy server?
130
131See the docs in L<LWP::UserAgent> on how to use the proxy. Short version:
132
133 $mech->proxy(['http', 'ftp'], 'http://proxy.example.com:8000/');
134
135or get the specs from the environment:
136
137 $mech->env_proxy();
138
139 # Environment set like so:
140 gopher_proxy=http://proxy.my.place/
141 wais_proxy=http://proxy.my.place/
142 no_proxy="localhost,my.domain"
143 export gopher_proxy wais_proxy no_proxy
144
145=head2 How can I see what fields are on the forms?
146
147Use the mech-dump utility, optionaly installed with Mechanize.
148
149 $ mech-dump --forms http://search.cpan.org
150 Dumping forms
151 GET http://search.cpan.org/search
152 query=
153 mode=all (option) [*all|module|dist|author]
154 <NONAME>=CPAN Search (submit)
155
156=head2 How do I get Mech to handle authentication?
157
158 use MIME::Base64;
159
160 my $agent = WWW::Mechanize->new();
161 my @args = (
162 Authorization => "Basic " .
163 MIME::Base64::encode( USER . ':' . PASS )
164 );
165
166 $agent->credentials( ADDRESS, REALM, USER, PASS );
167 $agent->get( URL, @args );
168
169If you want to use the credentials for all future requests, you can
170also use the L<LWP::UserAgent> C<default_header()> method instead
171of the extra arguments to C<get()>
172
173 $mech->default_header(
174 Authorization => 'Basic ' . encode_base64( USER . ':' . PASSWORD ) );
175
176=head2 How can I get WWW::Mechanize to execute this JavaScript?
177
178You can't. JavaScript is entirely client-based, and WWW::Mechanize
179is a client that doesn't understand JavaScript. See the top part
180of this FAQ.
181
182=head2 How do I check a checkbox that doesn't have a value defined?
183
184Set it to to the value of "on".
185
186 $mech->field( my_checkbox => 'on' );
187
188=head2 How do I handle frames?
189
190You don't deal with them as frames, per se, but as links. Extract
191them with
192
193 my @frame_links = $mech->find_link( tag => "frame" );
194
195=head2 How do I get a list of HTTP headers and their values?
196
197All L<HTTP::Headers> methods work on a L<HTTP::Response> object which is
198returned by the I<get()>, I<reload()>, I<response()/res()>, I<click()>,
199I<submit_form()>, and I<request()> methods.
200
201 my $mech = WWW::Mechanize->new( autocheck => 1 );
202 $mech->get( 'http://my.site.com' );
203 my $res = $mech->response();
204 for my $key ( $response->header_field_names() ) {
205 print $key, " : ", $response->header( $key ), "\n";
206 }
207
208=head2 How do I enable keep-alive?
209
210Since L<WWW::Mechanize> is a subclass of L<LWP::UserAgent>, you can
211use the same mechanism to enable keep-alive:
212
213 use LWP::ConnCache;
214 ...
215 $mech->conn_cache(LWP::ConnCache->new);
216
217=head2 How can I change/specify the action parameter of an HTML form?
218
219You can access the action of the form by utilizing the L<HTML::Form>
220object returned from one of the specifying form methods.
221
222Using C<< $mech->form_number($number) >>:
223
224 my $mech = WWW::mechanize->new;
225 $mech->get('http://someurlhere.com');
226 # Access the form using its Zero-Based Index by DOM order
227 $mech->form_number(0)->action('http://newAction'); #ABS URL
228
229Using C<< $mech->form_name($number) >>:
230
231 my $mech = WWW::mechanize->new;
232 $mech->get('http://someurlhere.com');
233 #Access the form using its Zero-Based Index by DOM order
234 $mech->form_name('trgForm')->action('http://newAction'); #ABS URL
235
236=head1 Why doesn't this work: Debugging your Mechanize program
237
238=head2 My Mech program doesn't work, but it works in the browser.
239
240Mechanize acts like a browser, but apparently something you're doing
241is not matching the browser's behavior. Maybe it's expecting a
242certain web client, or maybe you've not handling a field properly.
243For some reason, your Mech problem isn't doing exactly what the
244browser is doing, and when you find that, you'll have the answer.
245
246=head2 My Mech program gets these 500 errors.
247
248A 500 error from the web server says that the program on the server
249side died. Probably the web server program was expecting certain
250inputs that you didn't supply, and instead of handling it nicely,
251the program died.
252
253Whatever the cause of the 500 error, if it works in the browser,
254but not in your Mech program, you're not acting like the browser.
255See the previous question.
256
257=head2 Why doesn't my program handle this form correctly?
258
259Run F<mech-dump> on your page and see what it says.
260
261F<mech-dump> is a marvelous diagnostic tool for figuring out what forms
262and fields are on the page. Say you're scraping CNN.com, you'd get this:
263
264 $ mech-dump http://www.cnn.com/
265 GET http://search.cnn.com/cnn/search
266 source=cnn (hidden readonly)
267 invocationType=search/top (hidden readonly)
268 sites=web (radio) [*web/The Web ??|cnn/CNN.com ??]
269 query= (text)
270 <NONAME>=Search (submit)
271
272 POST http://cgi.money.cnn.com/servlets/quote_redirect
273 query= (text)
274 <NONAME>=GET (submit)
275
276 POST http://polls.cnn.com/poll
277 poll_id=2112 (hidden readonly)
278 question_1=<UNDEF> (radio) [1/Simplistic option|2/VIEW RESULTS]
279 <NONAME>=VOTE (submit)
280
281 GET http://search.cnn.com/cnn/search
282 source=cnn (hidden readonly)
283 invocationType=search/bottom (hidden readonly)
284 sites=web (radio) [*web/??CNN.com|cnn/??]
285 query= (text)
286 <NONAME>=Search (submit)
287
288Four forms, including the first one duplicated at the end. All the
289fields, all their defaults, lovingly generated by HTML::Form's C<dump>
290method.
291
292If you want to run F<mech-dump> on something that doesn't lend itself
293to a quick URL fetch, then use the C<save_content()> method to write
294the HTML to a file, and run F<mech-dump> on the file.
295
296=head2 Why don't https:// URLs work?
297
298You need either L<IO::Socket::SSL> or L<Crypt::SSLeay> installed.
299
300=head2 Why do I get "Input 'fieldname' is readonly"?
301
302You're trying to change the value of a hidden field and you have
303warnings on.
304
305First, make sure that you actually mean to change the field that you're
306changing, and that you don't have a typo. Usually, hidden variables are
307set by the site you're working on for a reason. If you change the value,
308you might be breaking some functionality by faking it out.
309
310If you really do want to change a hidden value, make the changes in a
311scope that has warnings turned off:
312
313 {
314 local $^W = 0;
315 $agent->field( name => $value );
316 }
317
318=head2 I tried to [such-and-such] and I got this weird error.
319
320Are you checking your errors?
321
322Are you sure?
323
324Are you checking that your action succeeded after every action?
325
326Are you sure?
327
328For example, if you try this:
329
330 $mech->get( "http://my.site.com" );
331 $mech->follow_link( "foo" );
332
333and the C<get> call fails for some reason, then the Mech internals
334will be unusable for the C<follow_link> and you'll get a weird
335error. You B<must>, after every action that GETs or POSTs a page,
336check that Mech succeeded, or all bets are off.
337
338 $mech->get( "http://my.site.com" );
339 die "Can't even get the home page: ", $mech->response->status_line
340 unless $mech->success;
341
342 $mech->follow_link( "foo" );
343 die "Foo link failed: ", $mech->response->status_line
344 unless $mech->success;
345
346=head2 How do I figure out why C<< $mech->get($url) >> doesn't work?
347
348There are many reasons why a C<< get() >> can fail. The server can take
349you to someplace you didn't expect. It can generate redirects which are
350not properly handled. You can get time-outs. Servers are down more often
351than you think! etc, etc, etc. A couple of places to start:
352
353=over 4
354
355=item 1 Check C<< $mech->status() >> after each call
356
357=item 2 Check the URL with C<< $mech->uri() >> to see where you ended up
358
359=item 3 Try debugging with C<< LWP::Debug >>.
360
361=back
362
363If things are really strange, turn on debugging with
364C<< use LWP::Debug qw(+); >>
365Just put this in the main program. This causes LWP to print out a trace
366of the HTTP traffic between client and server and can be used to figure
367out what is happening at the protocol level.
368
369It is also useful to set many traps to verify that processing is
370proceeding as expected. A Mech program should always have an "I didn't
371expect to get here" or "I don't recognize the page that I am processing"
372case and bail out.
373
374Since errors can be transient, by the time you notice that the error
375has occurred, it might not be possible to reproduce it manually. So
376for automated processing it is useful to email yourself the following
377information:
378
379=over 4
380
381=item * where processing is taking place
382
383=item * An Error Message
384
385=item * $mech->uri
386
387=item * $mech->content
388
389=back
390
391You can also save the content of the page with C<< $mech->save_content( 'filename.html' ); >>
392
393=head2 I submitted a form, but the server ignored everything! I got an empty form back!
394
395The post is handled by application software. It is common for PHP
396programmers to use the same file both to display a form and to process
397the arguments returned. So the first task of the application programmer
398is to decide whether there are arguments to processes. The program can
399check whether a particular parameter has been set, whether a hidden
400parameter has been set, or whether the submit button has been clicked.
401(There are probably other ways that I haven't thought of).
402
403In any case, if your form is not setting the parameter (e.g. the submit
404button) which the web application is keying on (and as an outsider there
405is no way to know what it is keying on), it will not notice that the form
406has been submitted. Try using C<< $mech->click() >> instead of
407C<< $mech->submit() >> or vice-versa.
408
409=head2 I've logged in to the server, but I get 500 errors when I try to get to protected content.
410
411Some web sites use distributed databases for their processing. It
412can take a few seconds for the login/session information to percolate
413through to all the servers. For human users with their slow reaction
414times, this is not a problem, but a Perl script can outrun the server.
415So try adding a C<sleep(5)> between logging in and actually doing anything
416(the optimal delay must be determined experimentally).
417
418=head2 Mech is a big memory pig! I'm running out of RAM!
419
420Mech keeps a history of every page, and the state it was in. It actually
421keeps a clone of the full Mech object at every step along the way.
422
423You can limit this stack size with the C<stack_depth> parm in the C<new()>
424constructor. If you set stack_size to 0, Mech will not keep any history.
425
426=head1 AUTHOR
427
428Copyright 2005-2009 Andy Lester C<< <andy at petdance.com> >>
429
430=cut