Website button click - Perl WWW::Mechanize

拥有回忆 提交于 2019-12-11 02:29:23

问题


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>

回答1:


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.




回答2:


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...});



回答3:


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

<button onlick="alert('hello');">Click Me</button>



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/8147006/website-button-click-perl-wwwmechanize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!