【问题标题】:How to click button using Mechanize in perl?如何在 perl 中使用 Mechanize 单击按钮?
【发布时间】:2016-06-23 07:11:53
【问题描述】:
<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。我想在perl 中使用Mechanize 单击名为full 的按钮。我怎样才能做到这一点?

我尝试过$mech-&gt;click("full")$mech-&gt;click_button(name =&gt; 'full')$mech-&gt;click_button(value =&gt; 'full'),但都没有奏效。他们都说“没有名称完整的可点击输入”。

【问题讨论】:

    标签: html perl button mechanize


    【解决方案1】:

    我下载你的 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)

    【讨论】:

    猜你喜欢
    • 2014-07-17
    • 2013-07-12
    • 1970-01-01
    • 2011-11-07
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多