Forum Moderators: coopster & phranque

Message Too Old, No Replies

WWW::Mechanize help

Need help with modifying and filling out a form

         

JohnSka7

8:41 pm on Apr 25, 2008 (gmt 0)

10+ Year Member



Hello everyone,

I am extremely new to perl (PHP programmer by trade) but am trying to leverage the power of www::mechanize for a project at work.

We use the PowerSchool system for our students, but we want to add the ability for parents and students to change their password on their own (right now we have to do it and it's a paper process). Unfortunately, we cannot directly update the database, so I'm going to use perl to log in to the form, navigate to the correct page, fill out the password field with a new password, then click submit.

I have stumbled, however, on the very first part of this. The login page for powerschool has 2 fields, a username(hidden) and password, but the password field is generated by javascript, a la


<script language="JavaScript" type="text/javascript">document.write('<input type="password" name="password" value="" size="50">');</script>

If I set up my mechanize like this:


$mech->get("pw.html");
print $mech->content;

It prints the whole page with the username field and password field.

However, if I do a


print $mech->form_dump();

I do not see the password field, and any manipulations I try to do via the $mech commands result in the following error in my logs:

No such field 'password'

An example I found from another PowerSchool user has them using HTML::Form and they use a push_input command to force the name of the field to be password, so I guess I'm looking for one of the following:

Can WWW::Mechanize to something similar?
or
Can I use a hybrid approach of HTML::Form and WWW::Mechanize? If so, how do I let them communicate?

Thanks!

Dabrowski

11:01 pm on Apr 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not used mech but I would guess that it's not processing the javascript so the password field never gets entered onto the actual form.

Protection again people like you doing things like this!

I'd attempt to post the data directly to the form's target as it doesn't appear to use any generated or hidden keys.

perl_diver

1:07 am on Apr 26, 2008 (gmt 0)

10+ Year Member



WWW::Mechanize does not support javascript at all. It clearly says that in the documentation.


You can use Win32::IE::Mechanize which does support javascript and should be able to handle this type of task.

[search.cpan.org...]

I have never used it so I can't help there but take a look and see if will do what you want. It does require Win32::OLE so that would have to be installed as well.

JohnSka7

4:46 pm on Apr 26, 2008 (gmt 0)

10+ Year Member



Thank you both for your quick replies.

Perl_Diver, I understand that mech doesn't support javascript. I was wondering if there was similar functionality as in HTML::Form where I can push a value onto a field (when I use mech to return the page contents, I do see the fields, so I know it's there), or is there a way that I can use HTML::Form to do the first part, and then once I log in get mech to continue?

I'm not really sure that, once I'm on the next page, if I can grab the cookie that was stored and whatnot...I'm still very new at all of this.

I will look into the ie version that you posted, though, and see if that can help.

Thanks!

perl_diver

9:04 pm on Apr 26, 2008 (gmt 0)

10+ Year Member



I don't know. I have no idea how that website you are accessing works. You might be able to login but if the site is using javascript you are going to run into problems again. They may have intentionaly coded the site that way to discourage bots, which is what you are making, from using the site.

Dabrowski

9:49 pm on Apr 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe you should look at using CGI instead. This will handle any cookies, and you can manually GET or POST the login information to the server.

If I were doing it, I'd first GET the login page. This will capture any cookies that the server send, session cookie for example.

Then try to POST the login data. If there is no more JavaScript to process this may work.

But really if the Win32::IE version will support the JavaScript I would try to use it, it's probably the best solution.

SeanW

1:54 pm on Apr 27, 2008 (gmt 0)

10+ Year Member



I don't have the man page handy, but you can access the underlying HTML::Form object, ie

my $form = $mech->form_number(1);

$form->blah...
$form->click();

Sean

phranque

6:12 am on Apr 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], JohnSka7!