2013-03-29 106 views
1

我是Casper JS的新手,我遇到了使用fill()方法登錄網站的問題。 在調試輸出中,我登錄後應該到達着陸頁的url。然而,然後送我到一個空白頁,並打破:CasperJS無法使用fill()方法登錄

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

我已經測試下面的代碼登錄到Facebook和它的工作。我還在下面的例子中使用Phantomjs版本的Casperjs代碼成功登錄了該網站。所以這個問題似乎特定於使用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

有點兒分不清這是怎麼回事所有的檢查員在那裏。如果您對共享網址或ID不熟悉,請將其改爲符合條件的內容,否則無法跟蹤此類問題。 – thealexbaron

回答

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() { 
    ... 
}