繁体   English   中英

用CasperJS填写表格

[英]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,当您未登录时在评论中单击“更多”时,会显示此表单。

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

好的,一切正常,感谢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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM