Website button click - Perl WWW::Mechanize
Asked Answered
P

5

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>
Perky answered 16/11, 2011 at 5:8 Comment(1)
Where's the mozrepl + WWW::Mechanize::Firefox answer? I've been waiting to upvote it.Octavia
M
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.

Malan answered 16/11, 2011 at 7:14 Comment(3)
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.Perky
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)Malan
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.Perky
N
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...});
Nomadize answered 16/11, 2011 at 8:46 Comment(1)
Thank you. I can fix my problem by using your method as above.Perky
J
0

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

<button onlick="alert('hello');">Click Me</button>
Jacintajacinth answered 16/11, 2011 at 5:20 Comment(1)
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.Perky
W
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.

Wershba answered 16/11, 2011 at 7:48 Comment(0)
R
-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.

Regale answered 16/11, 2011 at 5:19 Comment(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.Perky
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).Leslie

© 2022 - 2024 — McMap. All rights reserved.