2011-08-30 37 views
2
的HtmlUnit

我用Perl工作和WWW ::圖書館的HtmlUnit點擊訪問以下站點: https://www.cnatra.navy.mil/scheds/schedule_data.aspx?sq=VT-7如何模擬在「」鏈接使用Perl WWW ::

我可以加載頁面,點擊「查看時間表」和「搜索」按鈕,但我無法點擊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"; 

回答

1

由於沒有表,也不TR/TD或鏈接都有ID /名稱,您需要通過屬性搜索找到適當的元素。幸運的是,HTMLUnit僅爲此提供了一個API:getOneHtmlElementByAttribute

嘗試是這樣的(未測試,因爲我沒有訪問)

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