【问题标题】:CasperJS can't open link.hrefCasperJS 无法打开 link.href
【发布时间】:2013-11-07 13:32:49
【问题描述】:

我在页面上有一个链接按钮:

<a id="quote" href="quote.html" target="_blank">Quote</a>

一开始我点击链接:

casper.thenClick('#quote');

但我无法捕捉弹出窗口。所以我得到了链接的 url 并在当前窗口中打开它:

var url = '';

function getQuoteStartUrl() {
    var link = document.querySelector('a#quote');
    return link.getAttribute('href');
}

casper.thenOpen(url, function() {
    this.echo(url);
    this.echo(this.getTitle());
});

网址正确,但页面为空。然后我试试这个:

var url = 'http://quote.html';

casper.thenOpen(url, function() {
    this.echo(url);
    this.echo(this.getTitle());
});

它有效。 最后我知道它为什么不起作用:它在函数调用之前绑定步骤。所以我试试这个:

casper.then(function() {
    this.echo(url);
    this.thenOpen(url);
    this.echo(this.getTitle());
});

它也有效。

【问题讨论】:

    标签: casperjs


    【解决方案1】:

    您可以尝试以下方法吗?

    var url = '';
    
    casper.then(function() {
        url = this.getElementAttribute('a#quote', 'href');
    });
    
    casper.thenOpen(url, function() {
        this.echo(url);
        this.echo(this.getTitle());
    });
    

    看起来您可能没有在尝试加载之前设置 url。

    【讨论】:

    • 是的,你是对的,我将参数移到函数中,然后它就起作用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多