我有一个简单的网络应用程序(一个语音指南),它显示了一系列的术语作为链接。当用户单击其中一个时,它会触发音频播放器播放语音。
所以有一个点击事件,它触发一个获取音频文件的GET请求,然后由播放器加载并播放。
我想记录所有的GET请求,看看是否都成功了。我正在尝试使用PhantomJS来做到这一点。我把这个拼凑在一起:
var page = require('webpage').create(),
system = require('system'),
address = "http://d-college.cengage.com/demos/pronuncation_guide/index.html"
page.onResourceRequested = function (req) {
console.log('requested: ' + JSON.stringify(req, undefined, 4));
};
page.onResourceReceived = function (res) {
console.log('received: ' + JSON.stringify(res, undefined, 4));
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
$("a").click();
});
phantom.exit()
});
});
这成功地记录了页面加载上的所有资产以及包含的jquery。但我得到了:
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file://clicklog.js. Domains, protocols and ports must match.
我认为这本身并不是一个错误(参见:CasperJS and 'Unsafe JavaScript attempt to access frame with URL' error),我也不认为它会导致程序崩溃。
但是点击发生了吗?为什么没有记录产生的GET请求?
发布于 2015-01-07 22:28:35
那些印刷不安全的..。行是在1.9.8中引入的一个bug,它只在phantom.exit()
期间发生。它不会干扰剧本的其余部分。
您可能没有看到这些请求,因为您退出得太早。我的理解是不安全的。当您退出时仍然执行某些操作时,将打印行。当您单击链接并立即退出时,这适合您的情况。您应该让页面至少通过使用exit
延迟setTimeout
发送请求。
page.evaluate(...);
setTimeout(function(){
phantom.exit();
}, 1000);
https://stackoverflow.com/questions/27825383
复制相似问题