7

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

此示例代码无法按(我的)预期工作。不知道为什么:(

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"
4

1 回答 1

10

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

博客文章Webscraping with CasperJS and PhantomJS作为获取动态构造的 url 的示例可能会有所帮助。

于 2012-09-23T19:15:43.307 回答