2013-10-18 23 views
8

我是新來CasperJS填補形式,我有在登錄到這個網站http://weibo.com/login.php如何使用CasperJS不形標記

這裏的問題是我已經試過

this.fill('form#contact-form', { 
    'username': '[email protected]', 
    'password': 'anypassword', 

}, true); 

我因爲它沒有形式,所以不能使用它。

所以我嘗試了使用sendKeys的不同方法。

this.sendKeys('.W_input ', '[email protected]'); 

現在我在這裏的問題是,輸入文本中沒有ID,只有CLASS和用戶名和密碼都有相同的CLASS。我怎麼能只使用該類輸入到該文本框?或者有可能使用XPath的sendkey?

+0

'sendKeys'使用鍵盤事件輸入。這可能會導致一些奇怪的問題... – Sayakiss

回答

5

我用querySelector函數成功設置用戶名和密碼:

var casper = require("casper").create(); 
casper.start('http://www.weibo.com', function() { 
    this.evaluate(function(username, password) { 
     document.querySelector('input[node-type="username"]').value = username; 
     document.querySelector('input[node-type="password"]').value = password; 
     document.querySelector('.W_btn_g:eq(1)').click(); 
    }, 'your_username', 'your_password'); 

}); 

casper.then(function() { 
    this.wait(50000, function() { 
     this.echo("50s"); 
    }); 

}); 

casper.then(function() { 
    this.capture('weibo.png'); 
}); 

casper.run(); 
+0

說明會很好! – gsamaras

-1

這是簡單的卡斯帕爾js腳本填寫登錄表格 假設它已經提到了每個字段的ID

casper.evaluate(函數(用戶名,密碼){ document.querySelector('input#user_email')。value = username;

 document.querySelector('input#user_password').value = password; 
     document.querySelector('input#login-button').click(); 
    }, '[email protected]', 'password'); 
+1

請編輯您的答案以解釋如何解決問題。不提供代碼的答案,因爲他們不教什麼。也請將第一行代碼縮進4格,以便格式正確。謝謝! –