http://www.perlmonks.org?node_id=1008098

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using WWW::Mechanize::Firefox and want to follow a link on a page that uses javascript. I have tried using the follow_link function, Firefox shows the linked page but the Perl code hangs.

$mech->follow_link( n => 3 );

If I add synchronize => 0 then it continues but the html in $mech->contents is not updated.

$mech->follow_link( n => 3, synchronize => 0 );

I have also tried using the click call with the following code but I get the error message "Can't locate object method "__click" via package "WWW::Mechanize::Link"..."

my @links = $mech->links(); $mech->click({dom => $links[3]});

And with the following code the error message is "Can't locate object method "click" via package "WWW::Mechanize::Link"..."

my @links = $mech->links(); $links[3]->click();

Any help with the correct method to use would be very much appreciated.

Replies are listed 'Best First'.
Re: WWW::Mechanize::Firefox follow link with javascript
by Corion (Patriarch) on Dec 10, 2012 at 14:17 UTC
    You will have to inspect the HTML and Javascript of the page you're automating. As an alternative, you can also use a method that returns DOM objects instead of using a method that returns WWW::Mechanize::Link objects, as per the documentation.

      Thanks for the help. For the benefit of anyone else with this problem, the solution was to use the find_all_links_dom() function which returns the correct sort of link objects to use with the click function.