我正在使用 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);
关于javascript - 单击按钮应该会得到 Ajax 响应,但会在 CasperJS 中重新加载整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36432582/