使用 CasperJS 同时处理多个 window.prompts

Handlig multiple window.prompts at the same time using CasperJS

我需要重命名现有群组。创建组时只有一个 window 提示我使用下面的代码来处理

casper.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Enter new group name") {
        return ID;
    }
});

现在我想用新名称重命名创建的组 它会抛出 window 提示,如下所示

点击确定后,它会抛出另一个提示 window,如下所示

我该如何处理这种情况?

这是我在@artjom B 建议后的代码

casper.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Enter new group name") {
        return t;
    }
});

casper.setFilter("page.confirm", function (msg) {
    return msg.indexOf("Are you sure you want to rename group "+ID1+" to "+t+"?") !== -1;
});

casper.then(function () {
    this.click("span.label:nth-child(4)");
    console.log("Clicking on Rename button");
});

我怀疑第二个window是一个确认对话框(confirm()),所以你可以另外注册"page.confirm" filter:

casper.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Enter new group name") {
        return ID;
    }
});

casper.setFilter("page.confirm", function (msg) {
    return msg.indexOf("Are you sure you want to rename group \"" + groupName + " to \"" + newGroupName + "\"?") !== -1;
});

回调的return值决定是否确认。如果你不关心消息是什么,那么你可以return true.

此外,在您单击触发 prompt() 的按钮后可能需要稍等片刻,然后触发 confirm()。这些事件处理程序是异步的,它们 运行 在 CasperJS 控制流之外。因此,您需要让 CasperJS 在单击后稍等片刻,以便让这些处理程序执行。 casper.thenClick(selector).wait(100) 应该够了。

这可能对您有所帮助。我刚刚从第二个过滤器中删除了验证部分。

casper.then(function () {
this.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Rename group "+ID1) {          
        this.wait(5000)   
        this.echo("I'm Here")                                                                            
        return t;
    }
})
this.setFilter("page.confirm", function (msg) {                                        
    return true                           
})
this.click("span.label:nth-child(4)");
console.log("renamed the newly created group");
});