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>
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.
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.
You can add onclick event on this button. like this:
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:
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.
http://jsfiddle.net/fCdxR/1/
Quick example of how it works! Hope this solves your problem.