0

Hello everybody I am a newbie on javascript and I am trying to learn Casper.js/Phantom.js, but today I encountered a problem. I am trying to login in https://angel.co/login?utm_source=top_nav_home site with auto filling. My code fills username and password correctly however my

casper.getElementsByName("commit").click();

doesnt work at all. So my program doesnt login to the account. When I inspect element I dont see any id tag, I see name so I use getElementByName.My code is like this below

    phantom.casperPath = 'C:/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');
var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
    }
});

//First step is to open angellist
casper.start().thenOpen("https://angel.co/login", function () {
    console.log("angel website opened");
});


//lets fill username and pass login 
casper.then(function () {
    console.log("Login using username and password");
    this.evaluate(function () {
        document.getElementById("user_email").value = "myemail";
        document.getElementById("user_password").value = "mypassword";
      //casper.getElementsByName("commit").click();
      //THIS ONE ABOVE DOESN'T WORK  

    });
});

after this I take a screenshot but I see that it doesnt click the login button. I tried to click it using xpath too but it didn'work. Thanks for your help.

b2d2
  • 79
  • 1
  • 12
  • CasperJS has two contexts and the page context is sandboxed, so `casper` is not available inside of `casper.evaluate()`. Additionally, `element.click()` almost never works. You should use `casper.click("[name='commit']");` after the `casper.evaluate()` call. – Artjom B. Nov 22 '15 at 10:18
  • @Vohuman That's one of the issues, but it's the least one. – Artjom B. Nov 22 '15 at 10:19
  • Reopened the question. – Ram Nov 22 '15 at 12:06
  • @Vohuman Do you mean that email and password fields are not filled, judging by your screenshot? – Vaviloff Nov 23 '15 at 06:45
  • I solved the problem by using fairly easy code document.getElementsByName("commit")[0].click(); – b2d2 Nov 23 '15 at 07:40

1 Answers1

1

I solved the problem by using fairly easy code

document.getElementsByName("commit")[0].click(); since getElementsByName returns an array we have to give an adress to is( like [0] or other values). I wont edit or remove this question, for the other newbies on javascript like me. Thanks to everybody

b2d2
  • 79
  • 1
  • 12