# PODNAME: WWW::Mechanize::FAQ # ABSTRACT: Frequently Asked Questions about WWW::Mechanize __END__ =pod =encoding UTF-8 =head1 NAME WWW::Mechanize::FAQ - Frequently Asked Questions about WWW::Mechanize =head1 VERSION version 2.17 =head1 How to get help with WWW::Mechanize If your question isn't answered here in the FAQ, please turn to the communities at: =over =item * StackOverflow L =item * #lwp on irc.perl.org =item * L =item * The libwww-perl mailing list at L =back =head1 JavaScript =head2 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 HTML parts of the page. =head2 I thought Mech was supposed to work like a web browser. It does pretty much, but it doesn't support JavaScript. I added some basic attempts at picking up URLs in C calls and return them in C<< $mech->links >>. They work sometimes. 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. One typical use of Javascript is to perform argument checking before posting to the server. The URL you want is probably just buried in the Javascript function. Do a regular expression match on C<< $mech->content() >> to find the link that you want and C<< $mech->get >> it directly (this assumes that you know what you are looking for in advance). In more difficult cases, the Javascript is used for URL 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 URL. 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 URL. Step 2 - find the function call which created the URL - 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. Here's another approach that answers the question, "It works in Firefox, but why not Mech?" Everything the web server knows about the client is present in the HTTP request. If two requests are identical, the results should be identical. So the real question is "What is different between the mech request and the Firefox request?" The Firefox extension "Tamper Data" is an effective tool for examining the headers of the requests to the server. Compare that with what LWP is sending. Once the two are identical, the action of the server should be the same as well. I say "should", 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 LWP 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). Generally the problem is to be found in missing or incorrect POSTDATA 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, C<< $mech->add_header >> can be used to add the headers that you need. =head2 Which modules work like Mechanize and have JavaScript support? In no particular order: L, L, L, L, L =head1 How do I do X? =head2 Can I do [such-and-such] with WWW::Mechanize? If it's possible with LWP::UserAgent, then yes. WWW::Mechanize is a subclass of L, so all the wondrous magic of that class is inherited. =head2 How do I use WWW::Mechanize through a proxy server? See the docs in L 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 =head2 How can I see what fields are on the forms? Use the mech-dump utility, optionally 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] =CPAN Search (submit) =head2 How do I get Mech to handle authentication? use MIME::Base64; my $agent = WWW::Mechanize->new(); my @args = ( Authorization => "Basic " . MIME::Base64::encode( USER . ':' . PASS ) ); $agent->credentials( ADDRESS, REALM, USER, PASS ); $agent->get( URL, @args ); If you want to use the credentials for all future requests, you can also use the L C method instead of the extra arguments to C $mech->default_header( Authorization => 'Basic ' . encode_base64( USER . ':' . PASSWORD ) ); =head2 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 FAQ. =head2 How do I check a checkbox that doesn't have a value defined? Set it to the value of "on". $mech->field( my_checkbox => 'on' ); =head2 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" ); =head2 How do I get a list of HTTP headers and their values? All L methods work on a L object which is returned by the C, C, C, C, C, and C methods. my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->get( 'http://my.site.com' ); my $response = $mech->response(); for my $key ( $response->header_field_names() ) { print $key, " : ", $response->header( $key ), "\n"; } =head2 How do I enable keep-alive? Since L is a subclass of L, you can use the same mechanism to enable keep-alive: use LWP::ConnCache; ... $mech->conn_cache(LWP::ConnCache->new); =head2 How can I change/specify the action parameter of an HTML form? You can access the action of the form by utilizing the L object returned from one of the specifying form methods. Using C<< $mech->form_number($number) >>: my $mech = WWW::mechanize->new; $mech->get('http://someurlhere.com'); # Access the form using its Zero-Based Index by DOM order $mech->form_number(0)->action('http://newAction'); #ABS URL Using C<< $mech->form_name($number) >>: my $mech = WWW::mechanize->new; $mech->get('http://someurlhere.com'); #Access the form using its Zero-Based Index by DOM order $mech->form_name('trgForm')->action('http://newAction'); #ABS URL =head2 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. $mech->get( 'photo.jpg' ); $mech->save_content( '/path/to/my/directory/photo.jpg' ); You can also save any content directly to disk using the C<:content_file> flag to C, which is part of L. $mech->get( 'http://www.cpan.org/src/stable.tar.gz', ':content_file' => 'stable.tar.gz' ); =head2 How do I pick a specific value from a C<<