Casperjs无法动态打开网址?

joh*_*ohn 7 casperjs

我试图导航到从脚本本身创建的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();
    }
);
Run Code Online (Sandbox Code Playgroud)

结果是第二页不加载:

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"
Run Code Online (Sandbox Code Playgroud)

Sta*_*tan 10

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

使用CasperJS和PhantomJS进行Webscraping的博客文章可能有助于获取动态构建的URL.