【问题标题】:Casperjs can't open urls dynamically?Casperjs 不能动态打开网址?
【发布时间】:2015-11-18 04:05:05
【问题描述】:

我正在尝试导航到从脚本本身创建的 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"

【问题讨论】:

    标签: casperjs


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!事实上,如果我在 then(){} 中使用 this.open(),它就可以工作。不过这很令人不安,因为我担心随着我的项目扩大规模,它可能会导致代码非常难看:(
    • @johnjohn,前段时间我做了一个 CasperJS 的补丁(虽然不是一件小事),让它以逐步(临时)的方式工作。您可能可以根据自己的需要做同样的事情。我的版本的问题是它是前一段时间的,现在已经过时了,我不打算将它移植到最新的 CasperJS。
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2013-07-13
    相关资源
    最近更新 更多