2016-11-03 21 views
0

我想使用http://docs.casperjs.org/en/latest/modules/casper.html#waitforurl中的waitForUrl()函數。在發送登錄表單之後,casperjs應該等待下一頁被加載。在下面的代碼中,您會看到「第二個變體」,這是我嘗試對此進行編碼的方式,您也會看到發生的錯誤消息。 「第一個變體」正在工作,因此dashboard.png被捕獲。Casperjs和waitForUrl()等待下一頁

有人可以解釋「第二個變體」有什麼問題嗎?

// ... 

    // Type and send login Form 
    casper.then(function() { 
      this.evaluate(function(){          
        $("username").value="admin"; 
        $("password").value="pass#"; 
        $("login").click(); 
      }); 
    }); 

    // First variant (works) ----------------- 
    // casper.then(function() { 
    //  this.clickLabel('Dashboard', 'a'); 
    // }); 

    // Second variant (works not, error) ----------------- 
    // casper.waitForUrl(/\/admin\/index\.php/,function() { 
    //  this.clickLabel('Dashboard', 'a'); 
    //}); 
    // -> [error] [phantom] Wait timeout of 5000ms expired, exiting. 
    // -> Wait timeout of 5000ms expired, exiting. 


    casper.then(function(){ 
      this.capture('./dashboard.png'); 
    }); 
+0

http://docs.casperjs.org/en/latest/modules/casper.html#waitforurl –

+1

@Igor我已經在我的問題中發佈了該鏈接!我的代碼有什麼問題?看不到我的代碼和文檔之間的任何區別。 –

回答

0

我明白了!

「第二個變體」不是問題。之前的登錄步驟會導致錯誤。對於正確的登錄步驟,我需要等待一秒鐘以便casperjs設置cookie。

我也改變了填寫發送表格的方式。用下面的登錄步驟,「第二變體」的waitForUrl()正在正確的:

// Type and send login Form 
casper.wait(1000,function(){ 
      this.fill('.tl_login_form', { 
        username: "admin", 
        password: "pass#" 
      },true); 
}); 

此外,下列警告不見了!所以,這可能意味着,每次你得到這個警告,你不知道爲什麼,去檢查你的cookies:

[warning] [phantom] Loading resource failed with status=fail: http://mytestdomain.de 
+1

順便說一句,等待一段時間可能無法正常工作,如果登錄1秒後沒有響應,另一種選擇是使用類似'casper.waitForText('您已登錄',function()...'請參閱http:// docs.casperjs.org/en/latest/modules/casper.html#waitfortext它也可能會加快這一點 – Rippo