当应该从先前设置的变量打开下一页时,不会加载下一页

Next page doesn't load when it should be opened from a previously set variable

本文关键字:一页 加载 变量 设置      更新时间:2023-09-26

我正在从href属性中解析出一个网址,但是当我这样做时

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

显示空白。

我尝试访问的网页上的链接在新选项卡中打开,使用 casper 测试新选项卡比仅导航到同一选项卡上的新链接稍微复杂一些(我认为是这样),这就是为什么我在变量中获取链接然后尝试打开它。

我在这里错过了什么吗?

代码片段:

var href = '';
casper.then(function() {
    this.test.assertExists(x('//a[contains(@href, "SUBSTRING OF URL")]'), 'the element exists');
    href = casper.getElementAttribute(x('//a[contains(@href, "SUBSTRING OF URL")]'), 'href');
});
casper.thenOpen(href, function() {
    this.echo(getCurrentUrl());
});

因此,当我回显href时,它的值与我要导航到的网址相同。但getCurrentUrl显示about:blank.此外,URL 在不到一秒的时间内打开,但当我尝试casper.waitForUrl()时,它会在 5000 毫秒后超时。

Casper的工作原理是将所有请求推送到堆栈上,然后运行它们。所以在你称之为的时候:

casper.thenOpen(href, function() {
    this.echo(getCurrentUrl());
});

href还没有机会被定义。我相信以下重构是修复它的一种方法:

casper.then(function() {
  casper.open(href,function(){
    this.echo("Did it work this time?");
    });
});