Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Clicking a button with WWW::Mechanize

by tel2 (Pilgrim)
on Aug 12, 2014 at 00:42 UTC ( [id://1097053]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I haven't used WWW::Mechanize much, and I'm trying to get it to login to a site and make an entry. I can see that the logging on part is working fine, but there's a button that I want to click, and I'm not sure of the cleanest way to do that. Firefox's Firebug shows me that the code behind the button that I want to click looks like this:

<button class="button small" onclick="ga('send','event', 'Add CCL Acti +vity', 'Select', 'Add to Report button in Search Results')" type="sub +mit">Add to Report</button>
What's a clean way to click such a button? As you can see, it doesn't have a "name" attribute, and the "button small" class applies to several buttons on the webpage, so maybe I could click the Nth "button small" (right?), but is there a cleaner way to identify the field? One which won't break if they insert another "button small" above this one, perhaps? There are also several 'type="submit"' buttons on this webpage.

I've spent quite some time looking through the documentation and examples, and I can see various ways to click buttons, but nothing stands out as ideal for my situation, so I thought I should ask for expert advice.

Thanks.
tel2

Replies are listed 'Best First'.
Re: Clicking a button with WWW::Mechanize
by Gangabass (Vicar) on Aug 12, 2014 at 01:33 UTC
    WWW::Mechanize can't execute Javascript but you can find what info is actually submitted (with Firebug) and submit same fields from your script with $mech->post(...); or $mech->submit_form(...);
      Thanks Gangabass.

      That approach would have been my preference, from looking at Anonymous Monk's posts, it may be too hard for me.

      Hi again Gangabass,

      Partly for those who may follow, I just thought I should report back that I did end up using your proposed method with Firebug, and didn't need PhantomJS. I clicked Firebug's "Net" option, then "Persist" and "All", then clicked the "Add to Report" (JavaScript) button on the webpage. Firebug then showed "POST addcclactivityfromsearch" in its "URL" column, so I pointed my mouse at that and it gave me a full URL (i.e. "http://olr.ccli.com/search/addcclactivityfromsearch"). I could then click that link and look under the "Post" tab to see parameters and values sent, (OR right-click the link and "Copy POST Parameters" and paste them where I like). From that, I was able to remove some redundant parameters, and write this code, which seems to be working fine.

      $rc = $m->post('http://olr.ccli.com/search/addcclactivityfromsearch', { 'ActivityIndex' => -1, 'DigitalCount' => $use, 'ReportingPeriod' => $period, 'SongUniqueId' => $songid, 'X-Requested-With' => 'XMLHttpRequest' });

      Thanks for pointing me in the right direction!

      I might still use PhantomJS someday, but have some outstanding issues, as you can see below, so I might need to raise them again, somewhere more...visible.

      tel2

        ... I did end up using your proposed method with Firebug ...

        Also mentioned in WWW::Mechanize::FAQ -- its just full of nuggets :)

Re: Clicking a button with WWW::Mechanize (no javscript)
by Anonymous Monk on Aug 12, 2014 at 01:03 UTC
      Thanks Anonymous Monk!
Re: Clicking a button with WWW::Mechanize
by tel2 (Pilgrim) on Aug 12, 2014 at 07:42 UTC
    Hi again Anonymous Monk (and anyone else who may be able to help),

    Having had a look at the links you've provided, I decided to give WWW::Mechanize::PhantomJS a try.

    I installed the current version (0.06) on the commercial webhost that I use, using the cPanel webhosting control panel. The server is CentOS. I could not see any errors during the installation - it looked as if it installed OK. I then tried a couple of examples, including the first one at the top of the documentation page, and it fails. I then added warnings and strict, and here's the resulting code:

    #!/usr/bin/perl -w use strict; use WWW::Mechanize::PhantomJS; my $mech = WWW::Mechanize::PhantomJS->new(); $mech->get('http://google.com'); $mech->eval_in_page('alert("Hello PhantomJS")'); my $png= $mech->content_as_png();
    And here's the warning and error which result:
    Can't exec "phantomjs": No such file or directory at /home/tel2/perl5/ +lib/perl5/WWW/Mechanize/PhantomJS.pm line 101. Couldn't launch [| phantomjs /home/tel2/perl5/lib/perl5/WWW/Mechanize/ +PhantomJS/ghostdriver/main.js --PhantomJS=8910 --logLevel=OFF]: No su +ch file or directory / 0 at /home/tel2/perl5/lib/perl5/WWW/Mechanize/ +PhantomJS.pm line 101.

    Here is that section of code in PhantomJS.pm:

    # Launch PhantomJs $options{ launch_exe } ||= 'phantomjs'; (my $ghostdir_default= __FILE__) =~ s!\.pm$!!; $ghostdir_default= File::Spec->catfile( $ghostdir_default, 'ghostd +river', 'main.js' ); $options{ launch_ghostdir } ||= $ghostdir_default; $options{ launch_arg } ||= []; push @{ $options{ launch_arg }}, "--PhantomJS=$options{ port }"; push @{ $options{ launch_arg }}, "--logLevel=\U$options{ log }"; my $cmd= "| $options{ launch_exe } $options{ launch_ghostdir } @{ +$options{ launch_arg } }"; #warn $cmd; $options{ pid } ||= open my $fh, $cmd or die "Couldn't launch [$cmd]: $! / $?";
    The last line is line 101.

    I've googled a bit for answers, but nothing yet. Any ideas what the problem is, or how to fix this?

    Thanks again.
    tel2

      Did you install phantom.js, too?
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        No, choroba, I didn't realise it was a pre-requisite, but thanks for the pointer!

        I have now downloaded it from https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2, and temporarily extracted it to my local tmp directory.

        I can't see any installation instructions. The README.md file just says:
        "- **Easy to install**: {Download}(http://phantomjs.org/download.html), unpack, and start having fun in just 5 minutes."

        I haven't been able to find more installation instructions on the website.

        I assume I need to make it so WWW::Mechanize::PhantomJS can see & use it? How???

        If I run it from the command line, it seems to run, but I get a warning each time, e.g.:

        $ bin/phantomjs -h Fontconfig error: Cannot load default config file (and then I get the help listing)
        Any ideas about that? I saw it mentioned via Google, couldn't see an answer, yet.

        Thanks again.
        tel2

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097053]
Approved by GrandFather
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found