开发者

How to simulate a click on a "a" link with Perl WWW::HtmlUnit

开发者 https://www.devze.com 2023-04-01 01:27 出处:网络
I\'m working with Perl and the WWW::HtmlUnit library to access the following site: https://www.cnatra.navy.mil/scheds/schedule_data.aspx?sq=VT-7

I'm working with Perl and the WWW::HtmlUnit library to access the following site: https://www.cnatra.navy.mil/scheds/schedule_data.aspx?sq=VT-7

I can load the page, click on the "view Schedule" and "search" buttons, but I can not click on one of the numbered days in the ctrl calendar.

I have been looking at the click() function, but I must define the link I'd like to click on as an element before this function can be called.

Any ideas how I could actually get the program to find and click these links?

Here's the code from the site describing the link I'd like to click:

<td align="开发者_如何学Ccenter" style="width:14%;">
    <a href="javascript:__doPostBack('ctrlCalendar','4241')"
       style="color:Black" title="August 12">12</a>
</td>

Here's the simplified code I will put it in:

use WWW::HtmlUnit;
use Inline::Java;

my $webClient = WWW::HtmlUnit->new;
$webClient->setUseInsecureSSL(1);
my $page = $webClient->getPage("https://www.cnatra.navy.mil/scheds
/schedule_data.aspx?sq=vt-7");

###define $daylink element here. This is the calendar link I want to click

my $sched = $daylink->click();  
my $content = $sched->asXml;
print "\n$content\n\n";


Since neither the table nor TR/TD nor links have IDs/names, you need to find the appropriate element via attribute search. Luckily, HTMLUnit provides an API just for that: getOneHtmlElementByAttribute .

Try something like this (not tested as I have no access)

my $ancestor = $page->getBody();
my $daylink = $ancestor->getOneHtmlElementByAttribute('a', 'title', 'August 12');
0

精彩评论

暂无评论...
验证码 换一张
取 消