Search code examples
perlonclickwww-mechanize

Following links using WWW::Mechanize


I am trying to access an internal webpage to start and stop application using WWW::Mechanize. So far I am able to log in to the application successfully. My next action item is to identify a particular service from list of services and stop them.

The problem I am facing is I am unable to follow the link on the webpage. After looking at the HTML and link object, it is evident that there isn't a URL but an on click event.

Here is snippet of HTML

<ul>
<li>
<a href="#" id="j_id_id1:j_id_id9:2:j_id_id10" name="j_id_id1:j_id_id9:2:j_id_id10" onclick="A4J.AJAX.Submit('j_id_id1',event,{'similarityGroupingId':'j_id_id1:j_id_id9:2:j_id_id10','parameters':{'j_id_id1:j_id_id9:2:j_id_id10':'j_id_id1:j_id_id9:2:j_id_id10','ajaxSingle':'j_id_id1:j_id_id9:2:j_id_id10'} ,'containerId':'j_id_id0'} );return false;" style="color:#3BB9FF;">servicename</a>
</li>
</ul>

The link object dump is

$VAR1 = \bless( [
                   '#',
                   'servicename',
                   'j_id_id1:j_id_id9:2:j_id_id10',
                   'a',
                   bless( do{\(my $o = 'http://blah.services.jsf')}, 'URI::http' ),
                   {
                     'href' => '#',
                     'style' => 'color:#3BB9FF;',
                     'name' => 'j_id_id1:j_id_id9:2:j_id_id10',
                     'onclick' => 'A4J.AJAX.Submit(\'j_id_id1\',event,{\'similarityGroupingId\':\'j_id_id1:j_id_id9:2:j_id_id10\',\'parameters\':{\'j_id_id1:j_id_id9:2:j_id_id10\':\'j_id_id1:j_id_id9:2:j_id_id10\',\'ajaxSingle\':\'j_id_id1:j_id_id9:2:j_id_id10\'} ,\'containerId\':\'j_id_id0\',\'actionUrl\':\'/pages/services.jsf;jsessionid=NghBSoEJZKXbWcK0uVzcHvyebl8G_zSpf_Zu4uqrLI7xosHAnheK!1108773228\'} );return false;',
                     'id' => 'j_id_id1:j_id_id9:2:j_id_id10'
                   }
                 ], 'WWW::Mechanize::Link' );

Here is my code so far:

#!/usr/bin/perl
use strict;
use warnings; 
use Data::Dumper;
use WWW::Mechanize;            

my $username = 'myuser'; 
my $password = 'mypass';
my $url      = 'myinternalurl';

my $mech = WWW::Mechanize->new();
$mech->credentials($username,$password);
$mech->get($url);
my $link = $mech->find_link( text => 'servicename' );
#print Dumper \$link;
#$mech->follow_link( url => $link->url_abs() );
$mech->get($link->url_abs());
print $mech->text();

If I use follow_link, I get Link not found at log_in.pl line 16.. If I use get then I get back the same page. The problem is all these services appear to be hyperlinks but have the same url as my main url.

Here is a pic of the webpage:

enter image description here

When I manually click a service the Operations and Properties section change which allows the user to view Operation and Properties of the service they just clicked. Every service has different set of Operations and Properties.

How should I go about do this using perl? Is WWW::Mechanize the wrong tool for this one? Can anyone please suggest a solution or an alternate perl module that could help. Installing any CPAN module is not an issue. Working with latest version of perl is not an issue either. I have just started automating with perl and currently unaware of all the modules that could get the job done.

Looking forward to your guidance and help.

Note: If you feel there is any pertinent information, I may have missed, please leave a comment and I will update the question to add more details. I have modified proprietary information.


Solution

  • That button contains a Javascript onclick event, which will not work when using WWW::Mechanize.

    Per the docs:

    Please note that Mech does NOT support JavaScript, you need additional software for that. Please check "JavaScript" in WWW::Mechanize::FAQ for more.

    One alternative that does support Javascript in a forms is WWW::Mechanize::Firefox.