2015-11-25 128 views
0

我想用casperjs登錄本網站 http://forum.ngocrongonline.com/app/login.php使用casperjs登錄網站

這裏是我的test.js文件:

var casper = require('casper').create(); 

casper.start("http://forum.ngocrongonline.com/app/login.php", function() { 
    this.echo(this.getTitle()); 
    this.echo('Start to login ...'); 
}); 

casper.then(function() { 
    this.echo("Filling username and password ..."); 

    this.fillSelectors('form[name="login"]', { 
     'input[name="user"]': '<< my email >>', 
     'input[name="pass"]': '<< my password >>', 
     'input[name="server"]': '2' 
    }, true); 

    this.echo('Finish filling username and password!'); 
}); 

casper.thenEvaluate(function() { 
    document.querySelector('button[type="submit"]').submit(); 
    this.capture('pic1.png', { 
     top: 100, 
     left: 100, 
     width: 500, 
     height: 400 
    }); 
    this.echo('Finish capturing picture'); 
}); 


casper.run(); 

然而,當我嘗試運行腳本,似乎該腳本不運行casper.thenEvaluate部分。因此,我無法捕獲照片,看看我是否真的登錄過。 任何人都可以解釋如何完成此任務嗎?

回答

0

casper.evaluate()提供對PhantomJS和CasperJS中的沙箱化DOM上下文(頁面上下文)的訪問。 evaluate()中的this指的是全局對象,即window而不是casper。您無法訪問casper或頁面上下文外定義的任何其他變量,並且只能將原始對象傳入或傳出(文檔:PhantomJSCasperJS)。

既然你要做的操作是同步的,你可以簡單地將它移出功能:

casper.thenEvaluate(function() { 
    document.querySelector('button[type="submit"]').submit(); 
}); 
casper.then(function() { 
    this.capture('pic1.png', { 
     top: 100, 
     left: 100, 
     width: 500, 
     height: 400 
    }); 
    this.echo('Finish capturing picture'); 
});