我正在玩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());
});
通过这两种方式,它只是页面的完整提交,而不仅仅是生成建议。
答案 0 :(得分:2)
点击按钮(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()
无法处理按键等按键。您需要使用PhantomJS'page.sendEvent()
函数。它通常以下列方式工作,但在这种情况下似乎无法正常工作,因为它重新加载页面:
this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField', "Test", {keepFocus: true});
this.page.sendEvent("keypress", this.page.event.key.Enter);