简体   繁体   English

Perl用Javascript机械化

[英]Perl Mechanize with Javascript

I started working on perl mechanize and took a task to automate but got stuck with javascript in website. 我开始从事Perl机械化工作,并完成了一项自动化任务,但在网站中陷入了JavaScript问题。

the website I am trying my code on has a javascript based navigation (url remains same) between menu sections. 我尝试使用其代码的网站在菜单部分之间具有基于javascript的导航(URL保持不变)。 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. WWW::Mechanize不支持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. 对JavaScript进行反向工程,使用Mechanize从中抓取所需的任何数据,然后自己触发所有HTTP交互。 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). 在这种情况下,可能涉及提取“ ResultsSection”字符串,并将其与页面中其他位置(或可能是外部JavaScript文件)的某些数据进行匹配。
  • Switch to a different tool which does support JavaScript (such as Selenium). 切换到不支持JavaScript的其他工具(例如Selenium)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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