CasperJS无法打开link.href

时间:2013-11-06 12:33:05

标签: casperjs

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

<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());
});

它也有效。

1 个答案:

答案 0 :(得分:0)

你能试试以下吗?

var url = '';

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

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

在尝试加载之前,您似乎没有设置网址。

相关问题