views:

322

answers:

4

I am having problems with my Perl program. This program logs in to a specific web page and fills up the text area for the message and an input box for mobile numbers. Upon clicking the 'Send' button, the message will be sent to the specified number. I already got it to work for sending messages. But the problem is I can't make it work for receiving messages/replies. I'm using WWW::Mechanize module in Perl. Here is a part of my code (for receiving msgs):

$username = 'suezy';
$password = '123';
$url = 'http://..sample.cgi';

# ...

$mech->credentials($username, $password);  
$mech->get($url);

$mech->submit(); 

My problem is, the forms shows no names. There are two buttons in this form, but I can't select which button to click, since there are no name specified and the ids contains a space(e.g. form name='receive msg'..). I need to click on the second button, 'Receive'.

Question is, how will I be able to access the forms and buttons using mechanize module without using names?

+4  A: 

You can pass a form_number argument to the submit_form method.

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

ysth
Thanks! But how about for accessing buttons?
Suezy
How are you doing that? your example showed submit, so I suggested submit_form(form_number => xxx) instead; if you are using click or click_button, I suggested calling the form_number method beforehand.
ysth
+4  A: 

Have you tried to use HTTP Recorder?
Have a look at the documentation and try it to see if it gives a reasonable result for you.

weismat
Thanks! i'll check it out. :)
Suezy
I've got one question again please, how do i enable a radio button using Mechanize?
Suezy
The idea of using HTTP Recorder is to look at the resulting script - then you can see what is actually sent.<br> I guess it should be sth like $mech->set_visible( [ radio => 'KCRW' ] ); inside your recorded code.
weismat
+3  A: 

Seeing that there are only two buttons on your form, ysth's suggestion 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

A case of trial-and-error is more than adequate for you to figure out which button you're clicking.

EDIT

I'm assuming that the relevant form has already been selected. If not:

$mech->form_number($formNumber);

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

Zaid
click_button numbers are (according to the doc, anyway) relative to the current form; you'd have to call form_number first.
ysth
@ysth: Duly noted, answer updated
Zaid
Got it! thanks! :)
Suezy
Thanks soooo much for your help guys! ur right Zaid,, this should be a trial and error..This is a good start. :)
Suezy
I've got one question again please, how do i enable a radio button using Mechanize?
Suezy
@Suezy: Seeing that it wasn't part of your original post, why don't you post it as a new question?
Zaid
@Suezy: Don't forget to accept answers too!
Zaid
A: 

$mech->form_with_fields('username');

will select the form that contain a field named username. hth

free