如何模拟“a”上的点击与 Perl WWW::HtmlUnit 链接

发布于 2024-12-02 13:38:59 字数 1013 浏览 3 评论 0原文

我正在使用 Perl 和 WWW::HtmlUnit 库来访问以下站点: https://www.cnatra.navy.mil/scheds/schedule_data .aspx?sq=VT-7

我可以加载该页面,单击“查看时间表”和“搜索”按钮,但无法单击其中的编号日期之一ctrl 日历。

我一直在研究 click() 函数,但在调用此函数之前,我必须将要单击的链接定义为元素。

我有什么想法可以真正让程序找到并单击这些链接吗?

这是来自该网站的代码,描述了我要单击的链接:

<td align="center" style="width:14%;">
    <a href="javascript:__doPostBack('ctrlCalendar','4241')"
       style="color:Black" title="August 12">12</a>
</td>

这是我将其放入的简化代码:

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";

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="center" 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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-12-09 13:38:59

由于表、TR/TD 和链接都没有 ID/名称,因此您需要通过属性搜索找到适当的元素。幸运的是,HTMLUnit 专门为此提供了一个 API:getOneHtmlElementByAttribute

尝试这样的事情(未测试,因为我无权访问)

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

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文