1

我是 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

任何帮助表示赞赏。谢谢!

4

1 回答 1

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() {
   ...
}
于 2014-11-08T11:35:57.097 回答