单击一个按钮应该得到 Ajax 响应,但会在 CasperJS 中重新加载整个页面

Clicking a button should get an Ajax response, but reloads the whole page in CasperJS

我正在玩转 CasperJS 并试图在 https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung

上获取一些免费电子邮件别名

所以我有输入字段:"E-Mail-Wunschname:",我想在其中粘贴一个名称,然后单击 "Prüfen" 按钮,然后只抓取建议的帐户。

到目前为止我已经尝试过:

var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadPlugins: true,
        userAgent:('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Firefox/45.0')
    }

});

var mouse = require("mouse").create(casper);

casper.start('https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung').viewport(1200,1000);

casper.then(
    function() {               
        this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField',"Test");
        this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField',casper.page.event.key.Enter);       
        this.wait(5000);        
    }
);
casper.then(function() {
    this.wait(5000);
    this.capture('webde.png');
    console.log('clicked ok, new location is ' + this.getCurrentUrl());
});


casper.run();

我也厌倦了点击按钮:

casper.wait(6000, function() {
    this.evaluate(function(){                 
        document.querySelector('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField').value = "Test";           
        document.querySelector('#checkAvailabilityBtn').click();
            });
    });
    casper.then(function() {
        this.capture('webde.png');
        console.log('clicked ok, new location is ' + this.getCurrentUrl());
    });

这两种方式都只是页面的完整提交,而不仅仅是建议的生成。

点击按钮 (casper.click("#checkAvailabilityBtn")) 似乎效果不错。

这是完整的脚本:

var casper = require('casper').create();

casper.start('https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung').viewport(1200,1000);

casper.then(function() {               
    this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField', "Test"); 
    this.click("#checkAvailabilityBtn");
});
casper.wait(5000);
casper.then(function() {
    this.capture('test80_webde.png');
    console.log('clicked ok, new location is ' + this.getCurrentUrl());
});

casper.run();

顺便说一下,casper.sendKeys() 无法处理诸如 Enter 键之类的按键操作。您需要使用 PhantomJS 的 page.sendEvent() 函数。它按以下方式正常工作,但在这种情况下似乎无法正常工作,因为它重新加载了页面:

this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField', "Test", {keepFocus: true}); 
this.page.sendEvent("keypress", this.page.event.key.Enter);