0

Casperjs has some issue as it's not authenticating page when loaded initially, below is my code.

var casper = require('casper').create({
  verbose: false,
  logLevel: 'debug',
  pageSettings: {
    loadImages: false, // The WebPage instance used by Casper will
    loadPlugins: false, // use these settings
  }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
  this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
  this.echo("Page Error: " + msg, "ERROR");
});

//casper.options.viewportSize = {width: 1366, height: 667};

casper.start();
casper.options.pageSettings = {
  customHeaders:{
    'Authorization':'Basic '+btoa('username:password')
  }
}

casper.thenOpen('https://www.dmr.nd.gov/oilgas/basic/getwellprod.asp', function(status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    console.log("Getting Authenticated");
    this.echo(this.getTitle());
  }
});

casper.run();

I also tried using setHttpAuth with no luck. Here the Phantom version and resource.error message - Phantom version is 1.9.2 and ErrorCode is 6 and description is SSL handshake failed

IAmHomes
  • 513
  • 2
  • 11
  • 20
  • Acutally i have used the casper.capture() but it's a blank image.. – IAmHomes Mar 05 '15 at 20:23
  • So here it is sir, Phantom version is 1.9.2 and ErrorCode is 6 and description is SSL handshake failed – IAmHomes Mar 05 '15 at 20:31
  • possible duplicate of [CasperJS/PhantomJS doesn't load https page](http://stackoverflow.com/questions/26415188/casperjs-phantomjs-doesnt-load-https-page) – Artjom B. Mar 05 '15 at 20:32
  • So if i upgrade to Phantom 2 there seems to be problem with the CasperJs , and i did change the version function in bootstrap.js but still the same issue. Also i did try what you said in other post to use 'any' but it didn't work in this context – IAmHomes Mar 05 '15 at 20:35
  • I use CasperJS from the github master branch and it works flawlessly with PhantomJS 2, but you don't *need* to update. Just use the commandline options. I updated my answer. – Artjom B. Mar 05 '15 at 20:37
  • Still no luck with the command line options – IAmHomes Mar 05 '15 at 20:40
  • What is the new output that you see (terminal&screenshot)? – Artjom B. Mar 06 '15 at 10:48

1 Answers1

2

POODLE

The error message from the resource.error suggests that it is a POODLE bug. PhantomJS < 1.9.8 uses SSLv3 by default, but because of POODLE many web servers disabled SSLv3 support. So you need to tell PhantomJS/CasperJS that TLS should be used:

--ssl-protocol=tlsv1

You can also throw --ignore-ssl-errors=true in for good measure.

More information here: CasperJS/PhantomJS doesn't load https page

Basic Auth

There is no customHeaders option for pageSettings. You probably meant to use the PhantomJS' customHeaders option:

casper.page.customHeaders:{
    'Authorization': 'Basic '+btoa('username:password')
};

A better way would be to use the appropriate settings for this:

casper.options.pageSettings = {
    userName: username,
    password: password
};

You can also define this during creation.

If this doesn't solve your problem, check with the resource.requested and resource.received events whether the headers are set.

Callback arguments

Another issue with your code is that there is no status for the callback of thenOpen and other step functions. The last successfully loaded resource object is passed into the callback and not a string. So you cannot detect whether the page was loaded successfully using that.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222