简体   繁体   English

如何使用WWW:机械化登录

[英]How to use WWW:Mechanize to login

I want to login to a persian blogging service . 我想登录波斯博客服务。 This is my code: 这是我的代码:

#!/usr/bin/perl

use WWW::Mechanize;

$mech = WWW::Mechanize->new();
$url =  "http://blogfa.com/Desktop/Login.aspx?t=1";
$mech->get($url);

$result = $mech->submit_form(
form_name => 'aspnetForm', #name of the form
#instead of form name you can specify
#form_number => 1
fields      =>
{
 'master$ContentPlaceHolder1$Uid'    => 'my username', # name of the input field and value
 'master$ContentPlaceHolder1$Password'    => 'my password',
}
,'master$ContentPlaceHolder1$btnSubmit'    => 'ورود به بخش مدیریت' #name of the submit button
);

  $result->content();
if ($result =~ /میز کار/) {
print "Done\n"; }
else {
print "Failed!\n"; }

But it doesn't work at all. 但这根本不起作用。 What is the problem? 问题是什么?

The problem is, that WWW:Mechanize does not execute javascript. 问题是WWW:Mechanize不执行javascript。 Since the site you want to log in uses javascript for logging in, its not able to do that. 由于您要登录的站点使用JavaScript进行登录,因此它无法做到这一点。

You could fix that problem by using WWW::Mechanize::Firefox , which allows you to execute javascript. 您可以使用WWW::Mechanize::Firefox来解决该问题,该代码可以执行javascript。

This should work: 这应该工作:

my $mech = WWW::Mechanize->new();
$mech->get("http://blogfa.com/Desktop/Login.aspx?t=1");

$mech->submit_form(

    with_fields => {
        'master$ContentPlaceHolder1$Uid'       => 'my username',
        'master$ContentPlaceHolder1$Password'  => 'my password',
    },
    button => 'master$ContentPlaceHolder1$btnSubmit',
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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