3

I am beginner in CasperJs, i wrote the following code:

'use strict';

var casper = require('casper').create();

var username = "XXXXXXXX";
var password = "XXXXXXXX";

casper.start('https://instagram.com/accounts/login/', function() {

    this.echo(this.getTitle());
    console.log('Starting location is ' + this.getCurrentUrl());

});

casper.then(function() {

    /*this.fill('form[data-reactid=".0.0.1.0.1"]', {
        username: username,
        password: password
    }, true); i tried this*/

   /*this.fill('form[data-reactid=".0.0.1.0.1"]', {
       'input[id="lfFieldInputUsername"]': username,
       'input[id="lfFieldInputPassword"]': password
   }, true); i tried this too*/

    //i am trying this too because the page have only one form element
    this.fill('form', {
        username: username,
        password: password
    }, true);

});

casper.then(function() {
    console.log('Authentication ok, new location is ' + this.getCurrentUrl());
});

casper.run(function() {
    this.echo('end');
});

I found that it couldn't able to find the form element in the page loaded by CasperJs. In the Instagram form login, it does not have name/id/class/action of the form. while it works with twitter login when i used following snippet

this.fillSelectors('form.signin', {
          'input[name="session[username_or_email]"]':    email,
          'input[name="session[password]"]':             auth
      }, true);

And when i use this.fillSelectors then it shows the following error:

TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors') for this context.

When i use this.fill then it shows following error:

CasperError: Errors encountered while filling form: form not found

Any suggestions would be appreciated.

skmahawar
  • 313
  • 1
  • 13
  • Which PhantomJS and CasperJS version are you using? – Artjom B. Sep 01 '15 at 11:22
  • CasperJs version is 1.0.4 – skmahawar Sep 01 '15 at 11:28
  • (1) Which PhantomJS version do you use? (2) Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)). Maybe there are errors. – Artjom B. Sep 01 '15 at 11:31
  • Phantomjs version is 2.0.0 and i applied all events provided in the above comments where phantomjs gives warning ( [warning] [phantom] Loading resource failed with status=fail: https://instagram.com/accounts/login/ ) and shows same error (CasperError: Errors encountered while filling form: form not found) – skmahawar Sep 01 '15 at 11:50
  • I think your CasperJS installation uses a different PhantomJS version, because CasperJS 1.0.4 doesn't support PhantomJS 2.x. You would need to install CasperJS from git to use PhantomJS 2.0.0. – Artjom B. Sep 01 '15 at 11:55
  • 2
    @GauravDave Probably not a duplicate, as (assuming using the latest casperjs doesn't fix it) it is likely to be something specific to Instagram logins. (e.g. perhaps `waitFor` the form to be there, before trying to fill it in...) – Darren Cook Sep 01 '15 at 20:44

2 Answers2

0

I had the exact same issue. In the end I gave up looking for the form element. Instead of using this.fill('form', ...), I used standard javascript to fill each of the fields separately:

var username_field = document.getElementById('lfFieldInputUsername');
username_field.value = "XXXXXX";
var password_field = document.getElementById('lfFieldInputPassword');
password_field.value = "XXXXXX";

And instead of submitting the form, I sent a click event on the button element.

The following code is actually for PhantomJS, but you can probably convert it to the CasperJS API.

// Create a page object
var page = require('webpage').create();

// Open the page
page.open('https://instagram.com/accounts/login/', ...
// do other stuff like filling the form fields

// Send the click event
var point = page.evaluate(function () {
    var element = document.getElementsByTagName('button')[0];
    var rect = element.getBoundingClientRect();
    return {
        x: rect.left + Math.floor(rect.width / 2),
        y: rect.top + Math.floor(rect.height / 2)
    };
});
page.sendEvent('click', point.x, point.y);

The bad news is, after that I get a "password incorrect" error even though I'm sure the password is correct. I still haven't been able to fix that.

Maybe you'll have better luck than me using CasperJS.

You can see my full code here.

Community
  • 1
  • 1
Euripedes
  • 53
  • 7
0

here is my code :

casper.then(function () 
 {
        this.sendKeys('input[name="username"]', userName);
        this.sendKeys('input[name="password"]', pass);
        this.clickLabel('Log in','button');
 });
amir baqdadi
  • 174
  • 1
  • 1
  • 12