How can I access forms without a name or identifier using Perl WWW :: Mechanize?

I have problems with my Perl program. This program registers on a specific web page and fills in the text area for the message and the input field for mobile numbers. After clicking the "Send" button, the message will be sent to the specified number. I already received it to send messages. But the problem is that I cannot get it to work for receiving messages / replies. I am using the WWW :: Mechanize module in Perl. Here is part of my code (for receiving messages):

$username = 'suezy'; $password = '123'; $url = 'http://..sample.cgi'; # ... $mech->credentials($username, $password); $mech->get($url); $mech->submit(); 

My problem is that the forms do not contain names. There are two buttons in this form, but I can’t choose which button to press, because the name is not specified, and the identifiers contain a space (for example, the form name = ' receive messages ).). I need to click on the second "Get" button.

The question is, how can I access forms and buttons using a mechanization module without using names?

+7
perl www-mechanize
source share
4 answers

You can pass the form_number argument to the submit_form method.

Or call the form_number method to influence which form is used by later calls for a click or field.

+4
source share

Have you tried using an HTTP recorder ?
Look at the documentation and try to see if it gives you a reasonable result.

+4
source share

Seeing that there are only two buttons in your form, the ysth clause should be easy to implement.

 use strict; use warnings; use WWW::Mechanize; my $username = "suezy"; my $password = "123"; my $url = 'http://.../sample.cgi'; my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->credentials($username,$password); 

And then:

 $mech->click_button({number => 1}); # if the 'Receive' button is 1 

Or:

 $mech->click_button({number => 2}); # if the 'Receive' button is 2 

The case of trial and error is more than enough to find out which button you click.

EDIT

I assume that the appropriate form is already selected. If not:

 $mech->form_number($formNumber); 

where $formNumber is the form number on the page in question.

+3
source share

$mech->form_with_fields('username');

will select a form containing a username field. Hth

+1
source share

All Articles