2

I try to use the perl script to automate the interaction with a website.

I use module WWW::Mechanize to realize my design. But I cannot perform the button click in my perl script by using command as below.

$mech->click( $button [, $x, $y] ) 
$mech->click_button( ... ) 

It is because this button does not belongs to any form and without name, I can not call or locate this button in my perl script. How can I make it possible to click this button in my perl script like I interact with browser? Thank you.

<button class="button transactional" id="checkout-now"><span>Check Out Now</span></button>
mbx
  • 6,292
  • 6
  • 58
  • 91
Tommy Liu
  • 239
  • 4
  • 13
  • Where's the `mozrepl` + [`WWW::Mechanize::Firefox`](http://search.cpan.org/perldoc?WWW::Mechanize::Firefox) answer? I've been waiting to upvote it. – mob Nov 16 '11 at 16:27

5 Answers5

3

If button does not belong to any form it should have onclick event defined, like @Bilzac and @hijack mentioned. In this case you are not able to reproduce browser's behavior because WWW::Mechanize does only html analysis.

Dealing with JavaScript events it's more easy to implement browser's network activity rather then implementing whole JavaScript events and DOM interaction.

yko
  • 2,710
  • 13
  • 15
  • yko - Thank you for your answer. However, this website is not belongs to me. Therefore, I cannot change the html approach. How can I make it possible? Thank you. – Tommy Liu Nov 16 '11 at 07:33
  • THere's two ways: peek into JavaScript (which is boring, takes long time and requires JS knowledge) and peek into browser's network activity. Use Firefox or Chromium Network tab of developers tools/firbug to see whats going on when you click the button and implement that network activity in your script. This is actually the reason I don't use `WWW::Mechanize` family (though it's very handy sometimes) – yko Nov 16 '11 at 07:36
  • yko - Thank you for your sharing valuable experience. I greedy to ask that would you let me know how to use firebug to see what the script is doing? Thank you. – Tommy Liu Nov 16 '11 at 12:42
1

Well sometimes all you need is $mech->post() because it's harder to find what going on with JavaScript when you click some element.

So you need to find what request is performed when you click this button (you may use Firefox HttpFox for this) and after that construct same request using WWW::Mechanize:

$mech->post($request_url, Content => {FORM_FIELDS...});
gangabass
  • 10,607
  • 2
  • 23
  • 35
0

You can add onclick event on this button. like this:

<button onlick="alert('hello');">Click Me</button>
Bright
  • 72
  • 1
  • 2
  • Thank you for your reply. I had provided more information for my question. Please see the question again. I am sorry for my poor description in my question. – Tommy Liu Nov 16 '11 at 05:55
0

Clicking the button in a browser only runs a piece of JavaScript. You can't do that in WWW::Mechanize, because it has no JavaScript support.

What you could do, however, is find the JavaScript code that's being run when the button is clicked and write some Perl code to do the same thing. Unfortunately, it can sometimes be hard to find which JavaScript event handlers apply to an element. Even the otherwise excellent Firebug doesn't seem to have any simple way to list all event handlers associated with an element. (There does exist an Eventbug extension, but at a glance it doesn't seem to work very well.) The Visual Event bookmarklet might also be worth trying.

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
-1

I am not 100% sure what you are asking for, but I am going to swing at it. You do not need the button to be part of a form for any sort of interactivity. You can do this with just JavaScript & HTML.

$('#checkout-now').click( function() {
     alert('Hello!');
     //Do other stuff here, regarding the form! (Call Perl scripts etc.)
});

http://jsfiddle.net/fCdxR/1/

Quick example of how it works! Hope this solves your problem.

Bilzac
  • 555
  • 3
  • 9
  • 23
  • Thank you for your reply. I had provided more information for my question. Please see the question again. I am sorry for my poor description in my question. – Tommy Liu Nov 16 '11 at 06:01
  • He is trying to automate clicking links or something of this nature, he could be in an environment where he doesn't even have browser or javascript access (for example, trying to install packages on AIX or some OS without a repo manager). – tylerthemiler May 03 '13 at 21:45