我正在尝试导航到从脚本本身创建的网址。
此示例代码不起作用(我有)预期。无法弄清楚原因:(
var casper = require('casper').create({
viewportSize:{
width:1024, height:768
},
pageSettings:{
userAgent:'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11'
},
verbose:true
});
casper.on('open', function (location) {
console.log(location + ' loaded');
});
casper.start('http://www.google.com', function() {
this.test.assertTitle('Google', 'Google homepage title is the one expected');
});
casper.mytest = '';
casper.then(function () {
casper.mytest = 'http://www.yahoo.com';
});
casper.thenOpen(casper.mytest, function() {
this.test.assertTitle('Yahoo', 'Yahoo homepage title is the one expected');
});
casper.run(function () {
casper.exit();
}
);
结果是第二页没有加载:
http://www.google.com loaded
PASS Google homepage title is the one expected
loaded
FAIL Yahoo homepage title is the one expected
# type: assertTitle
# subject: ""
# expected: "Yahoo"
答案 0 :(得分:10)
我认为,问题的原因是,此时,当您为Yahoo注册thenOpen
步骤时,变量casper.mytest
为空。此时此值会进入CasperJS的步骤图,并且您在之前的步骤中更改源变量并不重要。
博客文章Webscraping with CasperJS and PhantomJS可能有助于获取动态构建的网址。