如何在perl中使用Mechanize单击按钮?

时间:2016-03-09 04:03:01

标签: html perl button mechanize

<input type="hidden" name="hiddenConsolidateFiles" value="0">
<button type="button" style="width:220px;" onClick="javascript:viewFullTextOfTreaty()" value="full" name="full" id="full">View Full Text of Treaty</button>
<button type="button" style="display:none;" onClick="javascript:viewFullText()" value="" id="sim">View Other Relevant Treaty Information</button>
<button type="button" style="display:none;" onClick="javascript:viewConsolidated()" value="consol" name="consol" id="consol">View Consolidated Version of Treaty</button>

我有以上HTML。我想在full中使用Mechanize点击名为perl的按钮。我怎样才能做到这一点?

我尝试了$mech->click("full")$mech->click_button(name => 'full')以及$mech->click_button(value => 'full'),但都没有效果。他们都说“没有名字已满的可点击输入”。

1 个答案:

答案 0 :(得分:0)

下载PDF时没问题:

my $url =
"http://ec.europa.eu/world/agreements/downloadFile.do?fullText=yes&treatyTransId=520";
{
    my $mech = WWW::Mechanize->new();
    $mech->get($url);

    my ($filename) = $mech->{res}->{_headers}->{"content-disposition"} =~ m{filename=([^"]+)};
    $mech->save_content($filename);
};

<强>更新

my $url =
  "http://daccess-ods.un.org/access.nsf/Get?Open&DS=A/HRC/WGAD/2015/28&Lang=E";

my $mech = WWW::Mechanize->new();
$mech->get($url);
$mech->save_content("1.htm");

#HTTP-EQUIV="refresh" URL
my ($tmp_url) = $mech->content() =~ m{URL=([^"]+)};
$mech->get( "http://daccess-ods.un.org" . $tmp_url );
$mech->save_content("2.htm");

my ($pdf_url) = $mech->content() =~ m{URL=([^"]+)};
my ($cookie_url) = $mech->content() =~ m{src\s*=\s*"([^"]+)};

#First we need to get cookies
$mech->get($cookie_url);

#Next we can load PDF file
$mech->get($pdf_url);
$mech->save_content("Result.pdf");

重要提示: 用正则表达式解析HTML真是个坏主意(我最喜欢的模块是HTML :: TreeBuilder :: LibXML)