Website Click Button - Perl WWW :: Mechanize

I am trying to use a perl script to automate interactions with a website.

I use the WWW :: Mechanize module to implement my design. But I cannot execute a button click in my perl script using the command as shown below.

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

This is because this button does not belong to any form and without a name, I cannot call or find this button in my perl script. How can I do this by clicking this button in my perl script, how do I interact with the browser? Thanks.

 <button class="button transactional" id="checkout-now"><span>Check Out Now</span></button> 
+2
source share
5 answers

If the button does not belong to any form, it must have a specific onclick event, for example, @Bilzac and @hijack. In this case, you cannot reproduce the behavior of the browser because WWW::Mechanize only performs html analysis.

Interaction with JavaScript events makes it easier to implement browser network activity, and then implement entire JavaScript events and interaction with the DOM.

+3
source

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

So, you need to find which request is executed when you click this button (you can use Firefox HttpFox for this), and after that build the same request using WWW :: Mechanize:

 $mech->post($request_url, Content => {FORM_FIELDS...}); 
+1
source

You can add onclick event to this button. eg:

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

When a button is clicked, only part of the JavaScript is executed in the browser. You cannot do this in WWW :: Mechanize because it does not have JavaScript support .

However, you might find JavaScript code that runs when you click a button and write Perl code to do the same. Unfortunately, it is sometimes difficult to find which JavaScript event handlers are applied to the element . Even different from other Firebugs , there seems to be no easy way to display all the event handlers associated with an element. (There is an Eventbug extension, but at first glance it doesn’t work very well.) A “visual event” can also be tried.

0
source

I'm not 100% sure what you are asking for, but I'm going to swing it. You do not need a button to be part of the form for any kind of interactivity. You can only do this with JavaScript and HTML.

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

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

A quick example of how this works! Hope this solves your problem.

-2
source

Source: https://habr.com/ru/post/901375/


All Articles