11

I recently tried to login into a webiste that forces me to accept cookies. I'm using phantomJs and casperJs. I wrote a little script that should handle the login, but it redirects me to a site that tells me I have to accept cookies. Email and password are just placeholders.

The site I want to login is https://de.buyvip.com/. But I need to click the button Anmelden mit Amazon so I can login with my amazon account. The other login form does not work. (That leads to this long url, I just copied it from my browser)

Can someone help me?

Here is the script:

    var casper = require("casper").create()
    var fs = require('fs');
    var page = "https://www.amazon.de/ap/signin?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&pageId=quarterdeckde&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&clientContext=280-1158662-4507036&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&marketPlaceId=A38GABX06X24K&openid.assoc_handle=quarterdeckde&openid.return_to=https%3A%2F%2Fde.buyvip.com%2Fsignin&openid.pape.max_auth_age=0&siteState=http%3A%2F%2Fde.buyvip.com%2Fhomepage%3Fhash%3DM";

    phantom.cookiesEnabled = true;

    casper.start(page, function()
    {
        console.log("started");
        this.fill('form#ap_signin_form', {
            'email' : 'myMail',
            'password' : 'myPass'
        }, true);
    });

casper.then(function()
{
    fs.write("test.html", this.getHTML(), "w");
});

    casper.run();
Maggie
  • 1,546
  • 16
  • 27
Ogofo
  • 356
  • 2
  • 6
  • 13
  • did you try to run the script with the command-line option ```--cookies-file=/path/to/cookies.txt```? – mike Nov 23 '12 at 10:59
  • Here is a good working example for PhantomJS: http://code-epicenter.com/how-to-login-amazon-using-phantomjs-working-example/ – MrD Aug 28 '15 at 12:50

2 Answers2

25

Maybe a bit later, but this is the answer:

casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');

the cookies are failing because amazon doesn't like the default casper's user agent, in my case: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) CasperJS/1.0.2+Phantomjs/1.7.0 Safari/534.34"

montes
  • 606
  • 10
  • 10
  • Yeah I tried something similar. Right now I cannot say how I solved it in detail. But thanks. Maybe this will help me in future :) – Ogofo Mar 19 '13 at 12:40
  • @Ogofo can you please search for your code and see how did you resolve it? I'm having the same issue. – MrD Jul 21 '15 at 10:24
  • @Mr.M I will check once I'm home. Expect an answer this evening. – Ogofo Jul 21 '15 at 10:27
4

My task was to make Phantom script which will login to Amazon webiste.

If you run Phantom with phantom.javascriptEnabled = true; and try to login Amazon using username and password, you will get JavaScript disabled message, meaning Javascript can not execute. When JS is not enabled, you are not able to login on Amazon, because cookies are not working.

Amazon executes small JS code to set and delete cookie before login, here is part of source code:

function setCookie(c_name,value,expiredays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+
            ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }

function checkCookieEnabled(nodeId)
        {
            setCookie('amznTest','1',null);
            if(getCookie('amznTest')){
                deleteCookie('amznTest');
            }else{
                document.getElementById(nodeId).style.display = 'block';
            }
        }
        checkCookieEnabled('message_warning');

After hours of workaround, you have to set page.settings.javascriptEnabled = true; and not only phantom.javascriptEnabled and everything worked smoothly (for me).

Enable javascript execution for phantom object:

phantom.cookiesEnabled = true;

Enable javascript execution for your page object (important):

 var webPage = require('webpage');
    var page = webPage.create();
    page.settings.javascriptEnabled = true;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';

Now, just submit form using your username and password, and you can login.

UPDATE:

Here is really good resource of How to login Amazon using PhantomJS. The same pattern can be used to login any other website.

Community
  • 1
  • 1
MrD
  • 2,423
  • 3
  • 33
  • 57
  • `phantom.cookiesEnabled = true` is the phantomjs default and therefore does not need to be set. `phantom.javascriptEnabled` also [defaults to true](http://phantomjs.org/api/webpage/property/settings.html). The original code sample also included `cookiesEnabled = true;` This is not a solution. – jbielick Aug 18 '15 at 23:44
  • How and why I put this answer? If you run Phantom with `phantom.javascriptEnabled = true;` and try to login Amazon using username and password, you will get JavaScript disabled message, meaning Javascript can not execute, and Amazon will refuse to login you, because cookies are not working. Amazon executes small JS code to set and delete cookie before login (check amazon source code on login page). After hours of workaround, you have to set `page.settings.javascriptEnabled = true;` and not only `phantom.javascriptEnabled` and everything worked smoothly. You can try it yourself. – MrD Aug 19 '15 at 09:44
  • That sounds like a whole lot of relevant information to include in your post instead of just vague instructions for how to set default settings. – jbielick Aug 19 '15 at 18:27
  • Upvoted! For casperjs users, this `page` object can be accessed after calling `runner.start()` as `runner.page.javascriptEnabled = true;` where `runner` is the casperjs instance that has been configured. Casperjs' `pageSettings` config option has a `javascriptEnabled` property, but this is **not the same as the aforementioned snippet!** The answer here would also work for Facebook.com login. – jbielick Aug 20 '15 at 13:21
  • 1
    Here is a really good example of How to login Amazon using PhantomJS: http://code-epicenter.com/how-to-login-amazon-using-phantomjs-working-example/ – MrD Aug 22 '15 at 19:48