使用 CasperJS 填写表格
Fill form with CasperJS
我正在尝试使用 CasperJS 填写此表格。我要输入邮箱,点击解锁此评论。
在 Chrome 上使用 Inspect Element 检查代码我得到以下信息:
<input id="email" type="text" class="emailAddr focusClear" value="Enter your email address" defaultvalue="Enter your email address" onblur="return ta.call('ta.common.search.defaultOnBlur', event);" onkeydown="return ta.util.keys.onEnterKeyClickSibling(event, '.submitBtn');" onfocus="ta.trackEventOnPage('overlay_registration_email_only', 'focused', '', '39415', false); return ta.call('ta.common.search.clearOnFocus', event);">
<div class="submitBtn rndBtn rndBtnGreen rndBtnLarge taLnk" onclick="ta.servlet.OverlayRegistration.submitEmailOnlySignup(event, ta.id('emailOnlySignup'), oreg_options);">Unlock this review</div>
我对 CasperJS 没有太多经验,我尝试使用以下代码提交此表单:
this.sendKeys('input[id="email"]', 'xxx@whatever');
我想我的选择器有问题,但我不确定。这是我正在检查的 URL,当您在未登录的情况下单击 'More' 评论时,您会得到此表格。
这是我使用的完整代码:
var casper = require("casper").create({
pageSettings: {
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0"
}
});
var url = 'http://www.tripadvisor.com/Restaurant_Review-g187497-d782630-Reviews-Restaurante_El_Mussol-Barcelona_Catalonia.html';
casper.start(url, function() {
this.echo('ok');
this.click("span.taLnk.hvrIE6.tr299457563.moreLink.ulBlueLinks");
this.echo('ok2');
this.capture('test-screen1.png');
}).wait(15000).then(function() {
this.echo('ok3');
this.capture('test-screen2.png');
/**this.fill('input.emailAddr', {
'email': 'xxx@whatever'
});*/
this.echo('ok4');
this.sendKeys('input[value="email"]', 'xxx@whatever');
this.echo('ok5');
this.capture('test-screen3.png');
}).wait(5000).then(function() {
this.echo('ok5');
this.capture('test-screen4.png');
});
/**fs.write(path,content,'w')**/
casper.run();
我从来没有达到 'ok5' 并且表格未填写。我怀疑我在 CSS 选择器中做错了什么。
由于此弹出窗口在 iframe 中打开,您无法访问电子邮件字段。
要使用 iframe,请查看文档:http://docs.casperjs.org/en/latest/modules/casper.html#withpopup
代码必须是这样的:
casper
.start(url, function() {
this.echo('ok');
this.click("span.taLnk.hvrIE6.tr299457563.moreLink.ulBlueLinks");
this.echo('ok2');
this.capture('test-screen1.png');
})
.waitForPopup(/Registration/, function() {
this.echo("Popup opened");
})
.withPopup(/Registration/, function () {
this.echo('ok5');
this.capture('test-screen3.png');
this.sendKeys('#email', 'xxx@whatever');
this.click('#emailOnlySignup .submitBtn');
})
好的,成功了,感谢您对 iframe 的提醒。如果您看到任何改进,请告诉我:)
var casper = require("casper").create({
pageSettings: {
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0"
}
});
var url= 'http://www.tripadvisor.com/Registration?source=OverlayRegistration&flow=EMAIL_ONLY_SIGNUP_OVERLAY&action=GET_EMAIL_ONLY_SIGNUP&pid=39415&category=overlay_registration_email_only&detail=782630&geo=187497&curUrl=http%253A__2F____2F__www__2E__tripadvisor__2E__com__2F__Restaurant__5F__Review__2D__g187497__2D__d782630__2D__Reviews__2D__Restaurante__5F__El__5F__Mussol__2D__Barcelona__5F__Catalonia__2E__html';
casper.start(url, function() {
this.echo('ok');
this.capture('test-screen1.png');
this.sendKeys('input[id="email"]', 'name@email.com');
this.echo('ok2');
this.capture('test-screen2.png');
this.click("div.submitBtn.rndBtn.rndBtnGreen.rndBtnLarge.taLnk")
this.echo('ok3');
this.then(function() {
var url = 'http://www.tripadvisor.com/Restaurant_Review-g187497-d782630-Reviews-Restaurante_El_Mussol-Barcelona_Catalonia.html';
this.start(url, function() {
this.echo('ok4');
this.click("span.taLnk.hvrIE6.tr299457563.moreLink.ulBlueLinks");
this.echo('ok5');
this.capture('test-screen3.png');
}).wait(15000).then(function() {
this.echo('ok6');
this.capture('test-screen4.png');
});
});
});
/**fs.write(path,content,'w')**/
casper.run();
我正在尝试使用 CasperJS 填写此表格。我要输入邮箱,点击解锁此评论。
在 Chrome 上使用 Inspect Element 检查代码我得到以下信息:
<input id="email" type="text" class="emailAddr focusClear" value="Enter your email address" defaultvalue="Enter your email address" onblur="return ta.call('ta.common.search.defaultOnBlur', event);" onkeydown="return ta.util.keys.onEnterKeyClickSibling(event, '.submitBtn');" onfocus="ta.trackEventOnPage('overlay_registration_email_only', 'focused', '', '39415', false); return ta.call('ta.common.search.clearOnFocus', event);">
<div class="submitBtn rndBtn rndBtnGreen rndBtnLarge taLnk" onclick="ta.servlet.OverlayRegistration.submitEmailOnlySignup(event, ta.id('emailOnlySignup'), oreg_options);">Unlock this review</div>
我对 CasperJS 没有太多经验,我尝试使用以下代码提交此表单:
this.sendKeys('input[id="email"]', 'xxx@whatever');
我想我的选择器有问题,但我不确定。这是我正在检查的 URL,当您在未登录的情况下单击 'More' 评论时,您会得到此表格。
这是我使用的完整代码:
var casper = require("casper").create({
pageSettings: {
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0"
}
});
var url = 'http://www.tripadvisor.com/Restaurant_Review-g187497-d782630-Reviews-Restaurante_El_Mussol-Barcelona_Catalonia.html';
casper.start(url, function() {
this.echo('ok');
this.click("span.taLnk.hvrIE6.tr299457563.moreLink.ulBlueLinks");
this.echo('ok2');
this.capture('test-screen1.png');
}).wait(15000).then(function() {
this.echo('ok3');
this.capture('test-screen2.png');
/**this.fill('input.emailAddr', {
'email': 'xxx@whatever'
});*/
this.echo('ok4');
this.sendKeys('input[value="email"]', 'xxx@whatever');
this.echo('ok5');
this.capture('test-screen3.png');
}).wait(5000).then(function() {
this.echo('ok5');
this.capture('test-screen4.png');
});
/**fs.write(path,content,'w')**/
casper.run();
我从来没有达到 'ok5' 并且表格未填写。我怀疑我在 CSS 选择器中做错了什么。
由于此弹出窗口在 iframe 中打开,您无法访问电子邮件字段。 要使用 iframe,请查看文档:http://docs.casperjs.org/en/latest/modules/casper.html#withpopup
代码必须是这样的:
casper
.start(url, function() {
this.echo('ok');
this.click("span.taLnk.hvrIE6.tr299457563.moreLink.ulBlueLinks");
this.echo('ok2');
this.capture('test-screen1.png');
})
.waitForPopup(/Registration/, function() {
this.echo("Popup opened");
})
.withPopup(/Registration/, function () {
this.echo('ok5');
this.capture('test-screen3.png');
this.sendKeys('#email', 'xxx@whatever');
this.click('#emailOnlySignup .submitBtn');
})
好的,成功了,感谢您对 iframe 的提醒。如果您看到任何改进,请告诉我:)
var casper = require("casper").create({
pageSettings: {
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0"
}
});
var url= 'http://www.tripadvisor.com/Registration?source=OverlayRegistration&flow=EMAIL_ONLY_SIGNUP_OVERLAY&action=GET_EMAIL_ONLY_SIGNUP&pid=39415&category=overlay_registration_email_only&detail=782630&geo=187497&curUrl=http%253A__2F____2F__www__2E__tripadvisor__2E__com__2F__Restaurant__5F__Review__2D__g187497__2D__d782630__2D__Reviews__2D__Restaurante__5F__El__5F__Mussol__2D__Barcelona__5F__Catalonia__2E__html';
casper.start(url, function() {
this.echo('ok');
this.capture('test-screen1.png');
this.sendKeys('input[id="email"]', 'name@email.com');
this.echo('ok2');
this.capture('test-screen2.png');
this.click("div.submitBtn.rndBtn.rndBtnGreen.rndBtnLarge.taLnk")
this.echo('ok3');
this.then(function() {
var url = 'http://www.tripadvisor.com/Restaurant_Review-g187497-d782630-Reviews-Restaurante_El_Mussol-Barcelona_Catalonia.html';
this.start(url, function() {
this.echo('ok4');
this.click("span.taLnk.hvrIE6.tr299457563.moreLink.ulBlueLinks");
this.echo('ok5');
this.capture('test-screen3.png');
}).wait(15000).then(function() {
this.echo('ok6');
this.capture('test-screen4.png');
});
});
});
/**fs.write(path,content,'w')**/
casper.run();