CasperJS无法使用fill()方法登录

时间:2013-03-29 17:32:57

标签: javascript forms login fill casperjs

我是Casper JS的新手,我遇到了fill()方法登录网站的问题。 在调试输出中,我到达登录页面的URL,该登录页面应该在我登录后到达。然而,它然后将我发送到一个空白页并打破:

[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=false

我已经测试了下面的代码登录到Facebook并且它有效。我还使用下面的Casperjs代码的Phantomjs版本成功登录到以下示例中的站点。所以这个问题似乎特定于使用Casperjs fill()方法登录。

1)这是html(出于安全目的我将其设为匿名):

//login form
<form id="***id_form***" class="loginBox" method="post" action="https://***login_link***/">

// input login
<input id="IDToken1" type="text" value="" name="IDToken1">

// input password
<input id="IDToken2" type="password" name="IDToken2">

2)这是我的Casperjs代码:

var start_url = 'https://***login_url***'

casper.start(start_url, function() {

    this.test.assertExists('form#***id_form***', 'form is found');
    this.fill('form#***id_form***', {
        'IDToken1': '***MY_ID***',
        'IDToken2': '***MY_PASSWORD***'
    }, true);
});

casper.run();

3)这是控制台输出(我隐藏了网址):

[info] [phantom] Starting...

[info] [phantom] Running suite: 2 steps

[debug] [phantom] opening url: https://***login_url***, HTTP GET

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] url changed to "https://***url***"

[debug] [phantom] Successfully injected Casper client-side utilities

[info] [phantom] Step 2/2 https://***url*** (HTTP 302)
PASS form is found

[info] [remote] attempting to fetch form element from selector: 'form#***id_form***'

[debug] [remote] Set "IDToken1" field value to ***My_ID***

[debug] [remote] Set "IDToken2" field value to ***My_PASSWORD***

[info] [remote] submitting form to https://***login_link***, HTTP POST

[info] [phantom] Step 2/2: done in 1211ms.

[debug] [phantom] Navigation requested: url=https://***login_link***, type=FormSubmitted, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***url***, type=FormSubmitted, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=http://***url***, type=FormSubmitted, lock=true, isMainFrame=true

[debug] [phantom] url changed to "http://***url***"

[debug] [phantom] Successfully injected Casper client-side utilities

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***landing_page***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] url changed to "https://***landing_page***"

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=false

[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=false

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

casper.start(start_url, function() {
    this.waitForSelector('form#***id_form***', function() {
        this.fill('form#***id_form***', {
            'IDToken1': '***MY_ID***',
            'IDToken2': '***MY_PASSWORD***'
        }, true);
    }
});

casper.then(function() {
   ...
}