Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What perl module is best to open a URL, search for a specific link (that doesn't change: say www.yourlink.com) and simulate a CLICK on that page? I don't want to open the link using get($url). And would this use the server's IP address or mine when I run the script from my site? Thanks.

Replies are listed 'Best First'.
Re: how to open page and follow link
by tlm (Prior) on Apr 11, 2005 at 02:54 UTC

    It's your lucky day: that's exactly what WWW::Mechanize was written for.

    the lowliest monk

Re: how to open page and follow link
by ikegami (Patriarch) on Apr 11, 2005 at 03:06 UTC

    Yes, WWW::Mechanize does exactly that, unless clicking the button involves JavaScript. If JavaScript was involved and you're on a Windows machine, I'd use Win32::IE::Mechanize.

    As for your second question, it would use the IP address of the machine on which the script is running. I presume you intend to do this from a CGI script. CGI scripts run on the web server, so it'll be the web server's IP address that will be used.

      Hi. Thanks for your help.

      I am running this code

      #!/usr/bin/perl use warnings; use strict; my $url = "(removed)"; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use lib "/home/name/public_html/lib/"; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get( $url ); print header, start_html(); print $mech->links;
      And is printing out
      WWW::Mechanize::Link=ARRAY(0xa430d5c)WWW::Mechanize::Link=ARRAY(0xa442 +a70)WWW::Mechanize::Link=ARRAY(0xa448fe4)WWW::Mechanize::Link=ARRAY(0 +xa448f00)WWW::Mechanize::Link=ARRAY(0xa444f24)WWW::Mechanize::Link=AR +RAY(0xa4445e0)WWW::Mechanize::Link=ARRAY(0xa442c5c)WWW::Mechanize::Li +nk=ARRAY(0xa4446c4)WWW::Mechanize::Link=ARRAY(0xa444f00)WWW::Mechaniz +e::Link=ARRAY(0xa44c7a0)WWW::Mechanize::Link=ARRAY(0xa448fa8)WWW::Mec +hanize::Link=ARRAY(0xa4450c8)WWW::Mechanize::Link=ARRAY(0xa444670)WWW +::Mechanize::Link=ARRAY(0xa449f44)WWW::Mechanize::Link=ARRAY(0xa44478 +4)WWW::Mechanize::Link=ARRAY(0xa44d31c)WWW::Mechanize::Link=ARRAY(0xa +449efc)WWW::Mechanize::Link=ARRAY(0xa44a004)WWW::Mechanize::Link=ARRA +Y(0xa44e664)WWW::Mechanize::Link=ARRAY(0xa4490f8)WWW::Mechanize::Link +=ARRAY(0xa449f80)WWW::Mechanize::Link=ARRAY(0xa44d664)WWW::Mechanize: +:Link=ARRAY(0xa449c5c)WWW::Mechanize::Link=ARRAY(0xa449ef0)WWW::Mecha +nize::Link=ARRAY(0xa44e6e8)WWW::Mechanize::Link=ARRAY(0xa451b98)WWW:: +Mechanize::Link=ARRAY(0xa44d574)WWW::Mechanize::Link=ARRAY(0xa449d1c) +WWW::Mechanize::Link=ARRAY(0xa4491a0)WWW::Mechanize::Link=ARRAY(0xa45 +2c70)
      Did I do something wrong?

      Thanks.

        links returns an array of objects of type WWW::Mechanize::Link, not an array of strings. Perhaps you want:

        print $_->url(), "\n" foreach $mech->links();
        Perhaps it'd be nice if WWW::Mechanize::Link had '""' overloaded to return the URL, when stringified. It would produce more "intuitive" results for normal use.

        You can currently produce that result by adding this to your code:

        use WWW::Mechanize; BEGIN { package WWW::Mechanize::Link; use overload '""' => \&url_abs; }

        OTOH it wouldn't do much good in your case, as you're trying to generate a HTML page of links. A simple, plain text, list of links then isn't much good.

Re: how to open page and follow link
by BUU (Prior) on Apr 11, 2005 at 06:41 UTC
    How exactly do you plan on simulating a "CLICK" without opening the link? What ends are you attempting to accomplish by simulating this "CLICK"?