简体   繁体   中英

WWW::Mechanize to access secure website

I have a script I am trying to log into an Oracle EBS page with and I believe I am one line away, (I could be wrong) from doing this. Below is my script and attached is the section of from data I think the answer is there some where.

I think its the submit and submit form line where I think I am failing. But if anyone sees other discrepancies, please let me know. I do see where the username in the script is in the form but not the password. I do believe the script has the right entry, but correct me if I am wrong.

Thanks in advance!

use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies;
my $outfile = "test";
my $url = "http ://url_address:portnumber /OA_HTML/Login";
my $username = 'johndoe';
my $password = 'johndoe123';
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_id('DefaultFormName');
$mech->field("usernameField", $username);
$mech->field("passwordField", $password);
$mech->submit_form(

form_id => "DefaultFormName",
fields => {
    usernameField => $username,
    passwordField => $password,
    '_FORM_SUBMIT_BUTTON' => "SubmitButtonofEPmL1A",
},
);
my $output_page = $mech->content();
print $output_page;
open(OUTFILE, ">$outfile");
binmode(OUTFILE, ":utf8");`enter code here`
print OUTFILE "$output_page";
close(OUTFILE); 

Form snipet:

"name="usernameField" size="0" type="text" value="johndoe">
< src="/OA_HTML/cabo/images/swan/t.gif" width="5"></td></tr><tr><td colspan="2"></td><td><span class="x2o">
(example: john.doe)</span></td></tr></table></td></tr><tr id="region41" align="left"><td id="region131" valign="top"><span class="x9g">*</span></td>
<td id="region51" valign="top"><span class="x9c">Password</span></td><td id="region61"><table id="passwordField__xc_" border="0" cellspacing="0" cellpadding="0"><tr><td align="right" nowrap></td><td></td>
<td valign="top" nowrap><input id="passwordField" title="Password" class=".LoginText" onchange="" name="passwordField" size="0" type="password">
< src="/OA_HTML/cabo/images/swan/t.gif" width="5"></td></tr><tr><td colspan="2"></td><td><span class="x2o">
(example: A1B2c3D4)</span></td></tr></table></td></tr><tr id="region132" align="left"><td id="region139">
</td><td id="region138"></td><td id="region133">
<button id="SubmitButton" title="Login" class="x7g" style="background-image:url(/OA_HTML/cabo/images/swan/btn-bg1.gif)" 
onclick="submitForm('DefaultFormName',1,{'_FORM_SUBMIT_BUTTON':'SubmitButtonofEPmL1A'});return false" type="submit">Login</button>< id="item11" src="/OA_HTML/cabo/images/swan/t.gif" width="2" height="1">"

WWW::Mechanize does not handle JavaScript. If JavaScript is involved (and it seems so, onclick="submitForm... ), use some other module (eg WWW::Mechanize::Firefox ).

You don't need this lines ( WWW::Mechanize will work with cookies for you automatically):

use HTTP::Cookies;
$mech->cookie_jar(HTTP::Cookies->new());

Next... Are you sure your link have this text : submitForm ? May be you need:

$mech->follow_link( url_regex => qr/submitForm/ );

I recommend to try this code instead (for submit part):

$mech->submit_form(

    form_id => "DefaultFormName",
    fields => {
        usernameField => $username,
        passwordField => $password,
        '_FORM_SUBMIT_BUTTON' => "SubmitButtonofEPmL1A",
    },
};

Update: Another way: you need to use tool like Firefox's HTTPFox add-on and find what data is send to the target site after each request. Next you simply send same data with $mech .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM