Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl WWW::Mechanize can't seem to find the right form or assign fields or click submit

Tags:

forms

perl

So I'm trying to create a perl script that logs in to SAP BusinessObjects Central Management Console (CMC) page but it doesn't even look like it's finding the right form or finding the right field or even clicking Submit.

Here's my code:

use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies;

my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get("http://myserver:8080/BOE/CMC");
$mech->form_name("_id2");
$mech->field("_id2:logon:CMS", "MYSERVER:6400");
$mech->field("_id2:logon:SAP_SYSTEM", "");
$mech->field("_id2:logon:SAP_CLIENT", "");
$mech->field("_id2:logon:USERNAME", "MYUSER");
$mech->field("_id2:logon:PASSWORD", "MYPWD");
$mech->field("_id2:logon:AUTH_TYPE", "secEnterprise");
$mech->click;
print $mech->content();

When I run it, I don't get any errors but the output I get is the login page again. Even more puzzling, it doesn't seem to be accepting the field values I send it (the output would display default values instead of the values I assign it). Putting in a wrong user or password doesn't change anything - no error but I just get the login page back with default values

I think the script itself is fine since I changed the necessary fields and I was able to log in to our Nagios page (the output page definitely shows Nagios details). I think the CMC page is not so simple, but I need help in figuring out how to make it work.

What I've tried:

1

use Data::Dumper;
print $mech->forms;
print Dumper($mech->forms());

What that gave me is: Current form is: WWW::Mechanize=HASH(0x243d828)

Part of the Dumper output is:

'attr' => {
         'target' => 'servletBridgeIframe',
         'style' => 'display:none;',
         'method' => 'post'
       },
'inputs' => []

I'm showing just that part of the Dumper output because it seems that's the relevant part. When I tried the same thing with our Nagios page, the 'attr' section had a 'name' field which the above doesn't. The Nagios page also had entries for 'inputs' such as 'useralias' and 'password' but the above doesn't have any entries.

2

$mech->form_number(1);

Since I wasn't sure I was referencing the form correctly, I just had it try using the first form it finds (the page only has one form anyway). My result was the same - no error and the output is the login page with default values.

3

I messed around with escaping (with '\') the underscore (_) and colon (:) in the field names.

I've searched and didn't find anything that said I had to escape any characters but it was worth a shot. All I know is, the Nagios page field names only contained letters and it worked.

I got field names from Chrome's developer tool. For example, the User Name form field showed:

<input type="text" id="_id2:logon:USERNAME" name="_id2:logon:USERNAME" value="Administrator">

I don't know if Mechanize has a problem with names starting with underscore or names containing colons.

4

$mech->click("_id2:logon:logonButton");

Since I wasn't sure the "Log On" button was being clicked I tried to specify it but it gave me an error:

No clickable input with name _id2:logon:logonButton at /usr/share/perl5/WWW/Mechanize.pm line 1676

That's probably because there is no name defined on the button (I used the id instead) but I thought it was worth a shot. Here's the code of the button:

<input type="submit" id="_id2:logon:logonButton" value="Log On" class="logonButtonNoHover logon_button_no_hover" onmouseover="this.className = 'logonButtonHover logon_button_hover';" onmouseout="this.className = 'logonButtonNoHover logon_button_no_hover';">

There's only one button on the form anyway so I shouldn't have needed to specify it (I didn't need to for the Nagios page)

5

The interactive shell of Mechanize

Here's the output when I tried to retrieve all forms on the page:

$ perl -MWWW::Mechanize::Shell -eshell
(no url)>get http://myserver:8080/BOE/CMC
Retrieving http://myserver:8080/BOE/CMC(200)
http://myserver:8080/BOE/CMC>forms
Form [1]
POST http://myserver:8080/BOE/CMC/1412201223/admin/logon.faces

Help!

I don't really know perl so I don't know how to troubleshoot this further - especially since I'm not seeing errors. If someone can direct me to other things to try, it would be helpful.

like image 345
Rim Avatar asked Feb 04 '26 21:02

Rim


1 Answers

In this age of DOM and Javascript, there's lots of things that can go wrong with Web automation. From your results, it looks like maybe the form is built in browser space, which can be really hard to deal with programmatically.

The way to be sure is to dump the original response and look at the form code it contains.

If that turns out to be your problem, your simplest recourse is something like Mozilla::Mechanize.

like image 78
darch Avatar answered Feb 06 '26 10:02

darch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!