[英]Fill form with CasperJS
我正在嘗試使用CasperJS填寫此表格。 我想輸入電子郵件,然后單擊“解鎖此評論”。
使用Chrome上的Inspect元素檢查代碼,我得到以下信息:
<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,當您未登錄時在評論中單擊“更多”時,會顯示此表單。
這是我正在使用的完整代碼:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.