用CasperJS填写表格

时间:2015-09-21 03:08:54

标签: javascript phantomjs casperjs

我正在尝试使用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选择器中我做错了什么。

2 个答案:

答案 0 :(得分:1)

由于在iframe中打开了此弹出式窗口,因此您无法访问电子邮件字段。 要使用iframe,请查看文档:{​​{3}}

代码必须是这样的:

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

答案 1 :(得分:0)

好的,让它工作,谢谢与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();