我有一个网页,可以在开始时加载.js
文件。在进行一些测试后,此脚本会重定向到另一个页面。
我需要使用casperjs
/ phantomjs
重现以下步骤。
在真实浏览器中:
以下是我的尝试:
casper.start( url );
casper.on("page.initialized", function(response){
this.evaluate(function(){
// the code I need to run before redirection ...
// it never enters in here
});
});
casper.run( function() {
casper.exit();
});
或
casper.start( url );
casper.then( function() {
this.evaluate(function(){
// the code I need to run before redirection ...
});
});
casper.run( function() {
casper.exit();
});
当我在控制台中运行该代码时:./phantomjs script.js
我在控制台中看到以下几行:
1
[debug] [phantom] Navigation requested: [WEBSITE_URL]
和
2
[debug] [phantom] url changed to [NEW_URL]
[debug] [phantom] start page is loaded
所以也许我应该找到一种方法来在1.和2之间运行我的js代码。
你怎么看?你有什么想法吗?谢谢!
修改:
我尝试过(按照建议):
casper.on("page.resource.requested", function(requestData, request){
casper.evaluate(function(){
// the code I need to run before redirection ...
});
});
但问题仍然是它没有进入evaluate
函数。