perlweb-scrapingwww-mechanize-firefox

using WWW::Mechanize::Firefox to use an onclick


I need to be able to follow an onclick on a webpage. Here's the code I have so far.

use strict;
use warnings;
use WWW::Mechanize::Firefox;

# Create a new instance of Mechanize
my $mech = WWW::Mechanize::Firefox->new();

# Go to page
$mech->get('http://www.website.com');

# TODO
# use a buttons onclick somehow....

Solution

  • # Go to page
    $mech->get('http://www.google.com');
    
    # Fill in the query box
    $mech->field( q => "turtles" );
    
    # Click the Feeling Lucky Button
    $mech->click( { xpath => '//button[@name="btnI"]' } );
    
    # Get the title of the result page
    my $page_title = $mech->title();
    
    print "$page_title\n";
    

    The documentation was pretty helpful in coming up with this.