How can I access forms without a name or id with Perl's WWW::Mechanize?
Asked Answered
C

4

7

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?

Cervin answered 14/2, 2010 at 8:56 Comment(0)
C
4

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.

Cantaloupe answered 14/2, 2010 at 9:12 Comment(2)
Thanks! But how about for accessing buttons?Cervin
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.Cantaloupe
D
4

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.

Decreasing answered 14/2, 2010 at 9:54 Comment(2)
I've got one question again please, how do i enable a radio button using Mechanize?Cervin
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.Decreasing
T
3

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.

Teillo answered 14/2, 2010 at 10:3 Comment(4)
click_button numbers are (according to the doc, anyway) relative to the current form; you'd have to call form_number first.Cantaloupe
Thanks soooo much for your help guys! ur right Zaid,, this should be a trial and error..This is a good start. :)Cervin
I've got one question again please, how do i enable a radio button using Mechanize?Cervin
@Suezy: Seeing that it wasn't part of your original post, why don't you post it as a new question?Teillo
A
1

$mech->form_with_fields('username');

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

Accessary answered 12/5, 2010 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.