2015-09-21 41 views
0

我試圖捕獲窗體,但它說「窗體找不到」。CasperJS:登錄窗體找不到

我沒了下文

this.test.assertExists('//*[@id="#loginForm"]', 'form is found'); 

this.fill('', { 
    login:"**********", 
    password:"**********", 

}, true); 

我的HTML代碼如下

<form id="#loginForm" class="form-signin" role="form" method="POST" action="/login"> 

我也嘗試過 「形式##登錄表單」,但我經常看到 「表格未找到」。

+0

還試圖形式[行動= 「/登錄」] –

+0

<表格ID =」 #loginForm「class =」form-signin「role =」form「method =」POST「action =」/ login「> –

+0

嗨,因爲答案幫助我,但我仍然無法點擊元素,因爲工具是隻是檢查html內容,在ajax調用之後,登錄表單打開,這是我的web元素都看不到的原因。你能幫我什麼,我應該做什麼來檢查每次頁面加載進一步進行Ajax調用。 –

回答

0

'//*[@id="#loginForm"]'是一個XPath表達式。幾乎所有的CasperJS功能支持XPath表達式,但你需要使用一個輔助函數XPath表達式和CSS選擇器來區分:

var x = require("casper").selectXPath; 
... 
this.test.assertExists(x('//*[@id="#loginForm"]'), 'form is found (xpath)'); 
this.test.assertExists('[id="#loginForm"]', 'form is found (css)'); 

其他問題可能是:

  • 形式是一個框架內,所以你需要切換到casper.withFrame()的框架。
  • 底層瀏覽器可能存在問題。您可以註冊不同的活動,以便了解可能會出錯的一瞥。您可以嘗試resource.error,page.error,remote.messagecasper.page.onResourceTimeout事件(Example)。
  • 您還應該頻繁地截取屏幕截圖(casper.capture())以查看您是否位於正確的頁面上或頁面是否已完全加載。如果不是,那麼你需要調查爲什麼可能是這樣的,以及之前的哪一步失敗。也許你只需要等待一段時間。

在殼體形式異步加載需要等待它出現這樣的:

var formSelector = x('//*[@id="#loginForm"]'); 
casper.waitForSelector(formSelector, function _then(){ 
    this.fill(formSelector, { 
     login: "**********", 
     password: "**********", 
    }, true); 
}, function _onTimeout(){ 
    this.test.fail("Login form loaded"); 
} /* , 5000 */); // timeout is 5 seconds by default