简体   繁体   中英

Fill form with CasperJS

I am trying to fill this form using CasperJS. I would like to enter the email and click Unlock this review.

在此处输入图片说明

Checking the code using the Inspect Element on Chrome I get the following:

<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>

I don't have a lot of experience with CasperJS, the code I am trying to use to submit this form is the following:

this.sendKeys('input[id="email"]', 'xxx@whatever');

I think I have a problem with the selector but I am not sure. This is the URL I am checking, and you get this form when you click 'More' in a review when you are not logged in.

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

This is the full code that I am using:

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();

I never reach 'ok5' and the form is not filled. I suspect it is in the CSS selector that I am doing something wrong.

You can't access to email field because of this popup opened in iframe. For working with iframes look to the docs: http://docs.casperjs.org/en/latest/modules/casper.html#withpopup

Code must be something like this:

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

Ok got it working, thanks for the heads up with the iframe. If you see any improvement let me know :)

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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