简体   繁体   English

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

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

I'm playing around with CasperJS and trying to catch some Free-Email Alias on https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung我正在玩 CasperJS 并试图在https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung上捕获一些免费电子邮件别名

So I have the input field: "E-Mail-Wunschname:" where I want to paste a name, then click the "Prüfen" button and then just scrape the suggested accounts.所以我有输入字段:“E-Mail-Wunschname:”,我想在其中粘贴一个名称,然后单击“Prüfen”按钮,然后只抓取建议的帐户。

So far I have tried that:到目前为止,我已经尝试过:

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

I also tired to click the button with:我也厌倦了点击按钮:

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

With both ways it's just a complete submit of the page and not just the generation of the suggestion.两种方式都只是完整提交页面,而不仅仅是生成建议。

Clicking on the button ( casper.click("#checkAvailabilityBtn") ) seems to work well.单击按钮( casper.click("#checkAvailabilityBtn") )似乎效果很好。

Here is the full script:这是完整的脚本:

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

By the way, casper.sendKeys() is not able to handle key presses such as the Enter key.顺便说一句, casper.sendKeys()无法处理诸如 Enter 键之类的按键操作。 You would need to use PhantomJS' page.sendEvent() function.您需要使用 PhantomJS 的page.sendEvent()函数。 It works normally in the following way, but doesn't seem to work correctly in this case, because it reloads the page:它按以下方式正常工作,但在这种情况下似乎无法正常工作,因为它会重新加载页面:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM