user2720970
user2720970

Reputation: 294

Casper.js Get webpage and display

Is there any way to use casper.js to login to a page then display the page? I can take screen shots I want it to be the actual page.

Here what I have so far, although Im sure its very wrong:

var casper = require('casper').create({
 viewportSize: {
    width: 1365,
    height: 768
},
// engine: 'slimmerjs',
verbose: true,
logLevel: "debug",
pageSettings: {
loadImages: false,//The script is much faster when this field is set to false
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36  (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
}
});

//First step is to open
casper.start().thenOpen("http://mysite/login", function() {
console.log("mysite website opened");
});

//Now we have to populate username and password, and submit the form
casper.then(function(){
console.log("Login using username and password");
this.evaluate(function(){
document.getElementById("user_email").value="me";
document.getElementById("user_password").value="password";
document.getElementById('new_user').submit();
});
});
casper.thenOpen('http://mysite/report/70?dashboard_id=2', function(){
console.log("Make a screenshot of morning info and save it as  pic.png"); 
this.wait(10000, function(){
this.captureSelector('pic.png','.highcharts- container');
});
});

casper.start("http://mysite/report/70?dashboard_id=2", function() {
this.page.switchToChildFrame(0);
this.page.switchToParentFrame();
});

casper.run(function() {
  this.exit();
});


casper.run();

Upvotes: 1

Views: 269

Answers (1)

DDeMartini
DDeMartini

Reputation: 337

If you want to capture the HTML, this is how you do it in Casper.

At the point you want this capture (after login..), within the same wait you have for the snapshot.. try this:

this.wait(10000, function(){
    this.captureSelector('pic.png','.highcharts- container');

    var HTML = document.createElement('textarea);
    HTML.innerHTML = this.getHTML();
    console.log(HTML.value);  // or use the variable for something else
});

Having used CasperJS for 4 years to perform web scraping, that was the only method I could devise that worked and gave me good parseable HTML.

Upvotes: 1

Related Questions