简体   繁体   中英

Perl Mechanize with Javascript

I started working on perl mechanize and took a task to automate but got stuck with javascript in website.

the website I am trying my code on has a javascript based navigation (url remains same) between menu sections. Take a look here

the code so far I have written gets me the link which redirects to the menu as shown in image.

$url="https://my.testingurl.com/nav/";
my $mech=WWW::Mechanize->new(autocheck => 1);
$mech->get($url);
$mech->form_name("LoginForm");
$mech->field('UserName','username');
$mech->field('UserPassword','password');
$mech->submit_form();
my $page=$mech->content;
if($page =~  /<meta\s+http-equiv="refresh"\s+content="\d+;\s*url=([^"+]*)"/mi)
{$url=$1 }
$mech->get($url);
print Dumper $mech->find_link(text_regex=>qr/View Results/);

and this is the output.

$VAR1 = bless( [
                 '#',
                 'View Results',
                 undef,
                 'a',
                 bless( do{\(my $o = 'https://my.testingurl.com/nav/')}, 'URI::https' ),
                 {
                   'onclick' => 'PageActionGet(\'ChangePage\',\'ResultsSection\',\'\',\'\', true)',
                   'href' => '#'
                 }
               ], 'WWW::Mechanize::Link' );

Now I am clueless how to proceed by clicking on the link shown in output and do the same with another part of navigation.

Please Help.

WWW::Mechanize doesn't support JavaScript. This leaves you with two basic options:

  • Reverse engineer the JavaScript, scrape any data you need out of it with Mechanize, then trigger any HTTP interactions yourself. In this case, it might involve extracting the "ResultsSection" string and matching it to some data from elsewhere in the page (or possibly an external JavaScript file).
  • Switch to a different tool which does support JavaScript (such as Selenium).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM