Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: LWP hit button to continue

by marto (Cardinal)
on Mar 23, 2021 at 15:44 UTC ( [id://11130200]=note: print w/replies, xml ) Need Help??


in reply to Re^2: LWP hit button to continue
in thread LWP hit button to continue

Quick and dirty example using WWW::Mechanize to get a fake page, click a button, print the title of the next page:

#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get('https://derpderpderp.com'); # fake URL $mech->click_button( number => 2 ) # click the second button on fake p +age #do whatever you want with the next page, e.g. print the title print $mech->title;

Replies are listed 'Best First'.
Re^4: LWP hit button to continue
by Anonymous Monk on Mar 23, 2021 at 16:18 UTC
    Thanks a lot, that's what I am looking for. However now I get an error:
    <h1>Software error:</h1> <pre>Can't call method &quot;click&quot; on an undefined value at C:/P +erl/site/lib/WWW/Mechanize.pm line 982, &lt;STDIN&gt; line 1. </pre> <p> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. </p>

    perl:
    use WWW::Mechanize;
    my $ua = WWW::Mechanize->new();
    $ua->get($url); # url page with button
    $ua->click_button( number => 1 ); # there is only one button
    print $ua->title;

      What is the output of:

      perl -MWWW::Mechanize -e 'print $WWW::Mechanize::VERSION ."\n";'

      What is the URL you're hitting.

Re^4: LWP hit button to continue
by Anonymous Monk on Mar 23, 2021 at 18:15 UTC
    Here is a simple example site:

    https://hungergj.home.xs4all.nl/test.htm

    This is the perl program "test button.pl":

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $ua = WWW::Mechanize->new(); $ua->get("https:\/\/hungergj.home.xs4all.nl\/test.htm"); $ua->click_button( number => 1 ); print $ua->title; print "=================\n";; $_=<STDIN>;
    The output is this time:
    "click_button: No form has been selected at test button.pl line 12."

      click_button says "Has the effect of clicking a button on the current form by specifying its attributes". The page at that URL doesn't have a form, so you get the expected error message.

      Just use the links instead as previously explained:

      #!/usr/bin/env perl use strict; use warnings; use WWW::Mechanize; my $ua = WWW::Mechanize->new(); $ua->get ('https://hungergj.home.xs4all.nl/test.htm'); my @links = $ua->links(); print $ua->get ($links[0]->url)->decoded_content;

      🦛

Re^4: LWP hit button to continue
by Anonymous Monk on Mar 23, 2021 at 16:41 UTC
    I am afraid giving the link would not be apropriate for this site :-) I thought there was a solution in general terms, so if that is not possible I will leave it at this...

      You didn't answer the other question. If sharing the URL isn't suitable you'll need to do some basic debugging.

Re^4: LWP hit button to continue
by Anonymous Monk on Mar 23, 2021 at 17:02 UTC
    Apologies, I overlooked that.
    Output after having to change ' to "
    1.89SCALAR(0x7db2d4)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found