javascriptphantomjscasperjs

Cookies popup stops login to Amazon with CasperJS


Cookies pop-up stops login to Amazon with CasperJS

I am trying to write a script using CasperJS to login into my Amazon account. As of now, in 2023, Amazon requires you to input the email first, and then it loads a new page for the password.

I managed to input my email and click "continue", however, right after that a pop appears that requires me to enable cookies: see screenshot

Unfortunately, as you can see, there is no option to "accept them all".

Furthermore, in the debug log it seems the page was loaded:

[debug] [phantom] :
 1. Mouse event 'click' on selector: input#continue
 2. Step anonymous 10/10 https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https://www.amazon.com/?ref_=nav_ya_signin&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&openid.ns=http://specs.openid.net/auth/2.0& (HTTP 200)
3. Navigation requested: url=https://www.amazon.com/ap/signin, type=FormSubmitted, willNavigate=true, isMainFrame=true
4. url changed to "https://www.amazon.com/ap/signin"
5. Successfully injected Casper client-side utilities
6. Step _step 11/11 https://www.amazon.com/ap/signin (HTTP 200)

But when I try downloading the contents of the loaded page like this:

var html = this.getPageContent();
this.download(html, 'signin.html');

it shows me the HTML source of an error page...

I get the same error even without inputting an email and just clicking continue.

I also tried to enable cookies on PhantomJS like this:

var casper = require('casper').create( {
    verbose: true, 
    logLevel: 'debug',
    userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
    pageSettings: {
        cookiesEnabled: true,
        loadImages: false,
        loadPlugins: false
    }
});

but it didn't work.

Is there a different way to enable the required cookies on PhantomJS for Amazon, bypass it, or something else? If I change to Selenium and use a different headless browser could it help?

Any help would be appreciated.

######################################################################################

Here's the rest of my code for reference (don't worry, I'll replace it with a link once I have my Git repository set up to make the question shorter):

Casper.start('https://www.amazon.com/', function() {
  // Wait for the page to load
  this.waitForSelector('#nav-link-accountList', function() {
    // Click on ....
    this.click('#nav-link-accountList');
  });
});

var email = "my email";
var pass = "test"; 

var email_field_id = '#ap_email';
var continue_button_id = 'input#continue';
var password_field_id = 'input#ap_password';
var submit_button_id = '#signInSubmit'

casper.then(function() {
  this.waitForSelector(email_field_id, function() {  
    this.sendKeys(email_field_id, email);
  });
});

casper.then(function() {
    this.wait(2000, function() {
        this.click(continue_button_id);
    });
});

casper.then(function() { 
    casper.wait(5000, function() {
        this.capture('login.png');
        var html = this.getPageContent();
        this.download(html, 'signin.html');
});

casper.run();

Solution

  • Welp, it might not be the most satisfying answer, but since I couldn't find an answer to this problem, I tried using Selenium instead, and I solved it in 15 minutes.

    No ReCAPTCHA (not the first time) and no cookies error.

    Bottom line- Use Selenium. It's better.