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();
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.
Here is really good resource of How to login Amazon using PhantomJS. The same pattern can be used to login any other website.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With