Casperjs无法动态打开网址?

时间:2012-09-23 16:06:13

标签: casperjs

我正在尝试导航到从脚本本身创建的网址。

此示例代码不起作用(我有)预期。无法弄清楚原因:(

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"

1 个答案:

答案 0 :(得分:10)

我认为,问题的原因是,此时,当您为Yahoo注册thenOpen步骤时,变量casper.mytest为空。此时此值会进入CasperJS的步骤图,并且您在之前的步骤中更改源变量并不重要。

博客文章Webscraping with CasperJS and PhantomJS可能有助于获取动态构建的网址。