gpt4 book ai didi

javascript - 使用 CasperJS 填写表单

转载 作者:行者123 更新时间:2023-12-03 08:50:42 25 4
gpt4 key购买 nike

我正在尝试使用 CasperJS 填写此表单。我想输入电子邮件并单击“解锁此评论”。

enter image description here

使用 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');

我认为选择器有问题,但我不确定。这是我正在检查的网址,当您未登录时在评论中单击“更多”时,您会收到此表单。

http://www.tripadvisor.com/Restaurant_Review-g187497-d782630-Reviews-Restaurante_El_Mussol-Barcelona_Catalonia.html

这是我正在使用的完整代码:

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

关于javascript - 使用 CasperJS 填写表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com