0

I am working on a script for DraftKings. The first thing I need to do is login.
The login in page looks like this:

As you can see there is a login button in the top right. When you click it a modal overlay displays with the login box.

Modal Overlay

I have a CasperJS script that looks like this:

var casper = require('casper').create({
    clientScripts: ["./jquery-2.1.4.min.js"],
    loadImages:false,
    verbose: true,
    logLevel: 'debug'
});

//set browser user agent
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');

//Open URL
casper.start('https://draftkings.com');

casper.thenEvaluate(function() {
    this.clickLabel('Sign-in', 'a');
    //.click('a[data-lp-signin-nav="1"]');
    this.capture('login.png');
});

casper.thenEvaluate(function() {
    //fill form

    //casper.evaluate(function(){
        casper.sendKeys('#Username', "xxxxx");   
        casper.sendKeys('#Password', "xxxxx");
        this.click('a[data-signin-submit="1"]');
        this.capture('screen.png');
    //});
});

casper.run(function() {
    //finish execution script 
    this.exit();
});

I start the page, then I try to click on the sign-in button to have the modal overlay appear, but when login.png does not show it displayed.

On another note I should be able to login without that displaying, but when I do it does not redirect me to the home page, and when I manually open the main page I am not logged in.

The console logs show no errors, and I can see that the forms items are being filled in and the button is being clicked.

Am I missing something here?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
urnotsam
  • 770
  • 7
  • 24

1 Answers1

2

There are a lot of problems with your script. Almost all of it isn't doing anything.

casper.evaluate() and casper.thenEvaluate() are the door into the DOM. They are sandboxed and have no access to outside functions. casper is not available inside of them and this refers to window. I suggest that you read the documentation to casper.evaluate() and page.evaluate() fully.

If you want to show possible errors, then you need to register to the resource.error, page.error, remote.message and casper.page.onResourceTimeout events (Example).

Then there is this problem that you're not waiting for anything to happen after you click something. You're immediately taking a screenshot after clicking, but you should put it into the next step.

Take this an refine it further:

var casper = require('casper').create({
    loadImages:false,
    verbose: true,
    logLevel: 'debug'
});

casper.start('https://draftkings.com');

casper.then(function() {
    this.clickLabel('Sign-in', 'a');
    //.click('a[data-lp-signin-nav="1"]');
});

casper.then(function() {
    this.capture('modal.png');
    this.sendKeys('#Username', "xxxxx");   
    this.sendKeys('#Password', "xxxxx");
    this.click('a[data-signin-submit="1"]');
});

casper.then(function() {
    this.capture('logged_in.png');
});

casper.run();

Don't include jQuery if you're not using it and the page already has a version. It might make break the page code, because of incompatibilities between the versions.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Thanks for the helpful response. I will do some more research. – urnotsam Nov 10 '15 at 20:19
  • I added a wait after this.clickLabel('sign-in', 'a'); for 10 seconds and the modal overlay still does not appear. Could it be I am not waiting long enough (10 seconds seems excessive) or could it be that the anchor tag does not have a href component? – urnotsam Nov 10 '15 at 20:54
  • You mean the sign in modal that should be visible in modal.png? It appears for me. I'm using CasperJS from git and PhantomJS 2.0.0. If you're using <1.9.8, then see [this](http://stackoverflow.com/a/26419531/1816580). – Artjom B. Nov 10 '15 at 20:56
  • I am using 1.9.8 because I was having problems with 2.0.0 but i will check out that thread thanks – urnotsam Nov 10 '15 at 21:00
  • I jjst upgraded to 2.0.0 with casper 1.0.4, but still do not see the overlay. Can you show me what you have that makes it display. Are you just using the code above? – urnotsam Nov 10 '15 at 21:45
  • I see [this](http://imgur.com/a/NCgjU) with PhantomJS 2.0.0 and newest CasperJS [from git](http://docs.casperjs.org/en/latest/installation.html#installing-from-git). [This](http://imgur.com/a/DPZ1X) is what I see when I run this code with PhantomJS 1.9.8 and CasperJS 1.1-beta3. – Artjom B. Nov 10 '15 at 21:57
  • I have tried both combinations of versions, but I still do not get the modal login box to display. I am using the code from above with a wait() before capturing the page, but still nothing. – urnotsam Nov 10 '15 at 22:00
  • [This](https://gist.github.com/artjomb/4afab4a6b85bcc8a4f4e) is the exact code I'm using. – Artjom B. Nov 10 '15 at 22:02
  • THANK YOU! It seems to be something with my adding of jquery that was screwing everything up. You have solved hours of problems for me and I will accept your answer for your help. – urnotsam Nov 10 '15 at 22:04
  • Oh, I should have posted the full script to begin with. Edited the answer. – Artjom B. Nov 10 '15 at 22:08
  • Only problem left with this code is that the password gets added to the username field even though the password id I am selecting is correct. – urnotsam Nov 10 '15 at 22:14
  • I really don't know why this is happening. You may try to pass username and password into the DOM context and set the input fields directly. – Artjom B. Nov 10 '15 at 22:33